Fileprovider doesn't work for image capture

倾然丶 夕夏残阳落幕 提交于 2019-12-23 12:44:53

问题


i've spent a lot of time trying to find a solution. I tried getExternalStoragePublicDirectory() and getExternalFilesDir() and many other things. But onActivityResult still brings error resultCode. It works when I switch from fileProvider to Uri.fromFile() only. But Android manual strictly recommends to use FileProvider for new APIs. Please tell me what I'm doing wrong.

Manifest fragment:

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

file-paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="my_images" path="Android/data/com.mydomain.myapp/files/Pictures" />
 <external-path name="pic" path="Pictures" /> </paths>

Java

File photoFile;
public void buttonContentChanger(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        try{
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            if (!storageDir.exists()) storageDir.mkdir();
            photoFile = File.createTempFile(imageFileName, ".jpg", storageDir);
        } catch (Exception e) {}
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this, "com.mydomain.myapp.fileprovider", photoFile);
      //          Uri photoURI1 = Uri.fromFile(photoFile); Working!!!!
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, 1);
            }
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bitmap bb = BitmapFactory.decodeFile("file:" + photoFile.getAbsolutePath(), null);
}

来源:https://stackoverflow.com/questions/39321746/fileprovider-doesnt-work-for-image-capture

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