FileProvider非常令人困惑 [英] FileProvider is very confusing

查看:61
本文介绍了FileProvider非常令人困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中捕获图像并将其设置为ImageView,然后将其上传到服务器.但是,每当我捕获图像时,图像都不会显示,并且当我尝试上传图像时,由于路径不包含图像,我会收到FileNotFoundException.

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);
    }

实际上,我将图像保存在内部存储目录"Pictures"中,并在其中存储了所有图像的应用程序名称后创建文件夹.但是在将图像设置为ImageView时,我得到了不同的文件路径,说

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.

推荐答案

这是怎么做的,效果很好.

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()确保您具有读写外部存储权限

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();
                }

            }
        });

然后最后是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;
        }
    }
}

获得

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

这篇关于FileProvider非常令人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆