Fileprovider不适用于图像捕获

时间:2016-09-04 21:43:28

标签: android android-fileprovider

我花了很多时间试图找到解决方案。我尝试了getExternalStoragePublicDirectory()和getExternalFilesDir()以及其他许多东西。但onActivityResult仍然带来错误resultCode。它仅在我从fileProvider切换到Uri.fromFile()时才有效。但Android手册严格建议将FileProvider用于新API。请告诉我我做错了什么。

清单片段:

<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>

文件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>

爪哇

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

0 个答案:

没有答案