Picking an image file from Gallery using FileProvider

前端 未结 2 1066
耶瑟儿~
耶瑟儿~ 2021-02-18 18:34

Compiling for Android N I\'ve faced an issue of FileProvider. I need to let user to pick image from gallery/take picture with camera then crop it to square.

2条回答
  •  眼角桃花
    2021-02-18 19:23

    Agreed with @x-code's answer you are not described very clear about issue although if you try to access another app's internal data then you must have permissions to do so.

    Files that rightfully belong to your app and should be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they are files that realistically don't provide value to the user outside your app.

    Actually i have found on documentation that SDK version 24 is now updated with many schemes and have massive changes in working with Files,from documentation the problem with file:// is described as..

    Passing file:// URIs outside the package domain may leave the receiver with an unaccessible path. Therefore, attempts to pass a file:// URI trigger a FileUriExposedException. The recommended way to share the content of a private file is using the FileProvider.

    Due to security reasons it is highly recommended to use Content:// instead of using file:// so basically use ContentProvider instead of FileProvider.

    A simple example of using it is below,

    in AndroidMenifest.xml

    
    
                
            
        
     
    

    And then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist.

    The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.

    res/xml/provider_paths.xml

    
    
        
    
    

    now to use it,

    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
            BuildConfig.APPLICATION_ID + ".provider",
            createImageFile());
    

    I have taken this from this blog so please read it for full understanding.Hope it helps everyone.

提交回复
热议问题