简体   繁体   中英

provider on /storage/emulated/0/


I know this is an oldie, but I do not seem to find a way to manage it...
I've to capture a Camera image for later IO processing, transferring it to a network share, but I fail lot before this... after some reading here, I found the way to actually do it on KitKat checking the running SDK, and it works.. but, now I'm testing it on a Nougat device as well..

for Nougat I've read a provider should be used, and I'm trying to...
the captured image MUST be stored in a particular folder which is
"/storage/emulated/0/InsulinPower/amSignTool/Data"
for later IO processing... so I think something similar to must be used, currently with no look..
I had a look at provider definition for all "area", but can not get the answer I need and more I get exceptions.. as defining the retrival URI I get
java.lang.reflect.InvocationTargetException
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/InsulinPower/amSignTool/Data/XCAMX-201805171828082115829615.jpg

in the file_paths I tried quiet everything, as you can see... but it only works if I do refer external-path and NOT external-path-files , which indeed seems to be the one I need..external-path the code is trivial....

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
    File pictureFile = null;
    try {
        pictureFile = GENERIC.amPictureFile(this);
    } catch (IOException ex) {
    // Error occurred while creating the File
    Toast.makeText(this, "Create file failed!", Toast.LENGTH_SHORT).show();
    }

if (pictureFile != null) {
    //https://stackoverflow.com/questions/40087944/content-uri-crashes-camera-on-android-kitkat
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
        this.imageToUploadUri = Uri.fromFile(pictureFile);
    } else {            
        this.imageToUploadUri = FileProvider.getUriForFile(this,"com.insulinpower.android.fileprovider", pictureFile);  // <-- THIS statement crashes with the indicated exception
    }
    //\  https://stackoverflow.com/questions/40087944/content-uri-crashes-camera-on-android-kitkat

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri);
    startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}


// file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Share folder under public external storage folder.The base folder is Environment.getExternalStorageDirectory()-->
    <!--external-path name="my_data" path="InsulinPower/amSignTool/Data/" /> -->
    <external-files-path name="my_data" path="/InsulinPower/amSignTool/Data/" />
    <!-- external-files-path name="my_data" path="InsulinPower/amSignTool/Data/" / -->
    <!-- external-files-path name="my_data" path="/" /-->
</paths>


// manifest
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.insulinpower.android.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"></meta-data>
</provider>

File storageDir = new File(Environment.getExternalStorageDirectory(),"");
returns "/storage/emulated/0" // valid, adding the /InsulinPower/amSignTool/Data suffix to go to the correct folder...

File file = this.getExternalFilesDir(null);
returns "/storage/emulated/0/Android/data/com.insulinpower.amsigntool/files" // not valid for me

any hint would be really appreciated to understand my errors, and please excuse my poor english..
TIA
Andrea

the captured image MUST be stored in a particular folder which is "/storage/emulated/0/InsulinPower/amSignTool/Data"

That is new File(Environment.getExternalStorageDirectory(), "InsulinPower/amSignTool/Data") .

but it only works if I do refer external-path and NOT external-path-files, which indeed seems to be the one I need

Correct. Try:

<external-path name="my_data" path="/InsulinPower/amSignTool/Data/" />

You definitely want <external-path> , not <external-files-path> , given where you want the file to go.

You also need:

cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

before you start the activity. Otherwise, the camera app has no rights to write to this location.

@CommonsWare: wow... thank's it works...
I just had to modify my pictureFile = GENERIC.amPictureFile(this);
to set the referencing module StringFileName to start as

this.m_PictureFilePath = "file:" + pictureFile.getAbsolutePath(); 


as I did not started it with "file:" as well...
and as GENERIC.amPictureFile(this) already points to Environment.getExternalStorageDirectory() + "/Insulin...../"
I had to modify

<external-path name="my_data" path="/InsulinPower/amSignTool/Data/" />


to

<external-path name="my_data" path="." />


as I think to understand the file creation already points to a sub-folder of the root, and having it doubbled in provider and in file creation should sub-nest the folding... or this is my understanding..

https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en helped as well...
thank you so much, you were very helpful indeed... really appreciated 'couse I just start to understand this fileprovider stuff
Andrea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM