【发布时间】: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