Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

android - FileProvider is very confusing

I have an app in which I capture image and set it to the ImageView and then upload it to the server. But whenever I capture images the image is not getting displayed and when I try to upload the image I get FileNotFoundException as the path does not contain the image.

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

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="Image Uploader" path="Android/data/com.example.android.imageuploader/files/Pictures" />
</paths>

When i create image file I use this,

private File createImageFile() {
        String timeStamp = new SimpleDateFormat(
                getString(R.string.time_stamp_format), Locale.getDefault())
                .format(new Date());
        String fileName = getString(R.string.file_name_format, timeStamp);
        File storageDirectory =
                new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
        if (!(storageDirectory.exists() || storageDirectory.mkdirs())) {
            Helper.showToastMessage(mContext, getString(R.string.warn_storage_dir));
        }
        return new File(storageDirectory.getPath() + File.separator + fileName);
    }

Actually I am saving the image in the internal storage directory 'Pictures' and in it I am creating folder after the app's name in which all the images are being saved. But while setting the image to the ImageView I am getting different file path say,

Image Uploader/Pictures/Image Uploader/20180406_101234.jpg

which is why the image is not being displayed as well as uploaded.

Where I am going wrong I am not able to figure that out. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how am doing, this works perfectly.

AndroidManifest.xml

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

provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path name="/storage/emulated/0" path="."/>
</paths>

createImageFile() make sure you have read and write external storage permission

private File createImageFile() throws IOException
{

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "MYAPPNAME-" + timeStamp + ".png";
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
            "YourAppFolder");
    File storageDir = new File(mediaStorageDir + "/Profile_Images");
    if (!storageDir.exists())
    {
        storageDir.mkdirs();
    }
    File image = new File(storageDir, imageFileName);
    return image;
}

click listener on button to take camera image

===Global Variables===
        Uri mUri; 
private static final int CAMERA_IMAGE_RESULT = 202;
    private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
===Global Variables===

        takeImageBTN.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                File file = null;
                try
                {
                    file = createImageFile();
                    mUri = FileProvider.getUriForFile(this,
                            CAPTURE_IMAGE_FILE_PROVIDER, file);

                    Log.d("uri", mUri.toString());
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
                    startActivityForResult(cameraIntent, CAMERA_IMAGE_RESULT);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }
        });

Then lastly onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode)
    {
        case CAMERA_IMAGE_RESULT:
        {
            if (resultCode == RESULT_OK)
            {
                if (mUri != null)
                {
                    Log.d("uriPath", mUri.getPath().replace("//", "/"));
                    String profileImageFilepath = mUri.getPath().replace("//", "/");
                    Log.d("path", profileImageFilepath);
                    profileIV.setImageURI(mUri);
                    /*Your Asynctask to upload Image using profileImageFilepath*/
                    new PostDataAsyncTask().execute(profileImageFilepath);

                }
            }
            break;
        }
    }
}

take run time permission for

  <!-- == External Storage == -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...