【问题标题】:FileProvider path creation and BitmapFactory.decodefile problems in AndroidAndroid中FileProvider路径创建和BitmapFactory.decodefile问题
【发布时间】:2017-05-19 19:03:59
【问题描述】:

我正在尝试使用 BitmapFactory.decodefile() 来创建一个缩小的 相机照片的版本并将其设置为我的框架布局中的图像视图。 我遵循 Android 开发人员的以下说明: https://developer.android.com/training/camera/photobasics.html 在这些说明中,文件被创建并存储在文件提供程序中,该文件提供程序包含一些以 xml 格式的元数据文件。不知何故,BitmapFactory.decodefile() 似乎无法访问该文件,该文件存储了其内容 uri 驻留在文件提供程序中的图片。 fileprovider 在 androidmanifest 文件中创建如下:

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

file_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/mypackagename/files/Pictures/" />
</paths>

图片所在的文件名是通过这个方法生成的:

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName,".jpg",storageDir);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
Log.d("absolute",""+image.getAbsolutePath());
return image;
}

代码启动一个意图,以便使用 startactivityforresult() 拍照,如下所示:

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (i.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File

}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile  (this,"mypackagename.fileprovider",photoFile);

i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(i,TAKE_PICTURE);
}
}

现在,onActivityForResult() 方法开始了,但是如果我像这样设置我的 if 语句 if(requestCode == TAKE_PICTURE && resultCode == RESULT_OK) 它不起作用。 我不知道为什么。 通过阅读有关 fileprovider 类的 android 参考文档,我看到了 我必须打开在我的意图中作为额外传递的 photoUri。 根据文档,我必须使用 ContentResolver.openFileDescriptor 打开它,这将返回一个 ParcelFileDescriptor。不知何故,这就是相机刚刚拍摄的照片所在的位置。 不知何故,我需要从这个 ParcelFileDescriptor 对象访问文件名并将其传递给 BitmapFactory.decodefile 以缩小图片位图并将其设置在我的 imageview 上。我不知道该怎么做

当尝试缩放图片位图时,我有以下代码返回 -1,这意味着“根据 BitmapFactory 类的 android 参考文档”“解码文件时出现问题”。我不知道为什么会有问题。这是返回 -1 的代码:

BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

photoW 和 photoH 都返回 -1。请记住,变量 mCurrentPhotoPath 是在 CreateImageFile() 方法中初始化的,一直在这个问题的顶部。

我也试过了,

BitmapFactory.decodeFile(photoFile.getAbsolutePath(), bmOptions);

但还是一样的结果,实际上 mCurrentPhotoPath 和 photoFile.getAbsolutePath() 是相等的字符串。

我认为文件提供程序及其元数据 xml 文件以某种方式隐藏了 BitmapFactory.decodefile() 的文件路径。

这张照片是我在测试应用程序时拍摄的,也存储在我的手机图片库中。

请提供任何建议或建议,因为我需要继续使用 tess-two 库并使用相机中的图片执行 OCR。

感谢您的建议

【问题讨论】:

  • 尝试为您创建的文件添加临时权限 - i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);然后再试一次。让我知道这是否有效。
  • 你是在运行时请求存储权限吗?

标签: android android-intent uri bitmapfactory android-fileprovider


【解决方案1】:

我遇到了完全相同的问题,这就是我处理它的方式:首先按照上面的建议设置i.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);,然后再启动相机。然后,不要使用BitmapFactory.decodeFile() 方法,因为它仅适用于使用Uri.fromFile() 检索的file:/// 类型的Uris,但不适用于使用FileProvider.getUriForFile() 检索的content:// 类型的Uris,这是API 所需的方式>= 24。

相反,使用 ContentResolver 打开一个 InputStream 并对图像进行如下解码:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
ContentResolver cr = getActivity().getContentResolver();
InputStream input = null;
InputStream input1 = null;
try {
    input = cr.openInputStream(photoUri);
    BitmapFactory.decodeStream(input, null, bmOptions);
    if (input != null) {
        input.close();
    }
} catch (Exception e) {
    e.printStackTrace();
}

int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
try {
    input1 = cr.openInputStream(photoUri);
    Bitmap takenImage = BitmapFactory.decodeStream(input1);                
    if (input1 != null) {
        input1.close();
    }
} catch (Exception e) {
    e.printStackTrace();
}

注意,为了获得位图,你必须打开第二个输入流,因为第一个不能被重用。

【讨论】:

  • BitmapFactory.decodeStream 正是我想要的!谢谢你
【解决方案2】:

我到此结束,因为我正在寻找一种方法来从内部存储中的图像的Uri 中获取Bitmap,使用FileProvider 编写。

(按照此答案将图像写入内部存储:https://stackoverflow.com/a/30172792/529663

我能够使用 GrAndroidDeveloper 的答案取回可读的 Bitmap 图像。方法如下:

ContentResolver cr = getApplicationContext().getContentResolver();
InputStream is = cr.openInputStream(uri);
Bitmap image = BitmapFactory.decodeStream(is);
if (is != null) is.close();

这对于需要Bitmapofficial facebook share 很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2012-12-22
    • 2017-11-19
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多