Path issue while saving and displaying an image in Android

£可爱£侵袭症+ 提交于 2019-12-12 02:42:42

问题


This might be my stupidity in writing paths but here is my code and it is failing to find configured root. getUriForFile is the one causing errors.

@Override
public void onClick(View v) {
    if (v.getId() == R.id.main_activity_camera_access_button) {
        Log.d(TAG, "clicked");
        Toast.makeText(getContext(), R.string.first_activity_toast_opening_camera, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(intent.resolveActivity(getActivity().getPackageManager()) != null) {
            mPhotoFile = null;
            try {
                mPhotoFile = createImageFile();
            } catch (IOException ex) {
                Log.d(TAG, "exception while creating photo file");
            }
            if(mPhotoFile != null){
                mfileUri = FileProvider.getUriForFile(getContext(), "edu.lclark.imagineyourwall.fileprovider", mPhotoFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mfileUri); // set the image file name

                // start the image capture Intent
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        }
            //mfileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    }
}

I have a xml directory under res and in there is my my_paths.xml. Here is the code for the my_paths.xml.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
    path="Android/data/edu.lclark.imagineyourwall.name/files/Pictures" />
</paths>

Also, my manifest looks like this, at least the provider part.

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="edu.lclark.imagineyourwall.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/my_paths"></meta-data>
    </provider>

回答1:


I think you're following Android Developer documentation for this right? If you're not targeting Android N (Nougat), it is safe to remove the getUriForFile line and modifyintent.putExtra(MediaStore.EXTRA_OUTPUT, mfileUri) to intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile)).

According to Android developer documentation;

For more recent apps targeting Android N and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore, we now present a more generic way of storing images using a FileProvider.



来源:https://stackoverflow.com/questions/39135994/path-issue-while-saving-and-displaying-an-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!