Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file:// scheme and content:// scheme confusions || ANDROID

As android is increasing its security and changing some stuff now a days to make Android OS more secure to Vulnerabilities, its a good thing to understand new structure for developers to get into it. I have been away from development from last couple of years.I just noticed that some stuff have been changed e.g file scheme of URI.

Case 1 : : I am launching camera intent with custom URI mentioned below.

var takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(packageManager) != null) {
        // Create the File where the photo should go
        var photoFile: File? = null
        try {
            photoFile = createImageFile()
        } catch (ex: IOException) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            val photoURI: Uri = FileProvider.getUriForFile(this,
                    BuildConfig.APPLICATION_ID + ".fileprovider",
                    photoFile)
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
        }
    }

photoURI is custom URI. When i took picture and check URI it has file:// in start from where i can get file path and i can do some stuff with it(i am uploading it to server)

Case 2

I am launching simple intent to pick image from gallery

        val intent = Intent()
    intent.type = "image/*"
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
    intent.action = Intent.ACTION_GET_CONTENT
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), TYPE_IMAGE)

and now when i pick image and check URI its has content:// in start. And the trouble starts when i try to get file path from it. i have been searching into SOF but none of solutions worked for me.

My main purpose it get file path is that i want to pick file from device and upload it to server.But with content:// scheme i can not make a file from URI and thus can not send this file server.

i just read CommonsWareBlog about scheme system. it cleared few things but i still don't know how can i switch from content:// too file:// in my onActivityResult!

i would like to mention it again that current answers on SOF are not working for me that's why i have to ask this question in details.

I am using 7.0 device emulator

This is just an example of my problem in actual i am picking up PDF and DOCX files and want to upload them on server and after picking files i have URI with content:// scheme and i can not make a file and send to server.

i am not sure what i am missing about schemes. I hope this question will help many other developers which are new to file system.

pleasure try to explain by code example according to my problem of picking file from intent (if you like)

like image 258
Zaeem Sattar Avatar asked Oct 17 '22 23:10

Zaeem Sattar


1 Answers

Since Android 7.0, returning file:// scheme is discouraged (android changelog), since it can leak files from private folders. You can still encounter it, especially when using apps that target below Nougat.

Short answer is when you query for Intent.ACTION_GET_CONTENT You receive content:// uri that you can ContentResolver.openInputStream(Uri) on to obtain the data. This ensures item you picked is read-only, and masks it's real source - it might've been a file, provided remotely or generated by provider all together.

like image 97
Pawel Avatar answered Oct 20 '22 05:10

Pawel