当前位置: 首页 > 知识库问答 >
问题:

文件提供程序路径创建和位图工厂。Android中的解码文件问题

冯宏恺
2023-03-14

我正在尝试使用BitmapFactory。decodefile()以创建相机照片的缩小版本,并将其设置为我的framelayout中的imageview。我遵循Android开发者的以下说明:https://developer.android.com/training/camera/photobasics.html在这些说明中,文件被创建并存储在一个fileprovider中,fileprovider保存一些xml格式的元数据文件。不知怎么的,位图工厂。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>

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

当尝试缩放图片位图时,我有以下返回-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);

但仍然是相同的结果,实际上是McCurrentPhotoPath和photoFile。getAbsolutePath()是相等的字符串。

我认为带有元数据xml文件的文件提供者以某种方式对BitmapFactory隐藏了文件路径。解码文件()。

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

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

谢谢你的建议

共有2个答案

唐声
2023-03-14

我在这里结束,因为我正在寻找一种方法,从内部存储器中图像的Uri获取Bitmap,该图像是用FileProvider编写的。

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

使用GrAndroidDeveloper的答案,我可以得到一个可读的位图图像。以下是方法:

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

这对于官方facebook共享非常有用,因为它需要一个位图。

厍胤运
2023-03-14

我遇到了完全相同的问题,这就是我处理它的方式:首先设置i.set标志(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);如上所述,在启动相机之前。然后,不要使用BitmapFactory.decodeFile()方法,因为它只适用于使用Uri.fromFile()检索到的Uris类型,但不适用于file:///code>代码>内容://类型的Uris检索使用FileProvider.getUriForFile()这是API所需的方式

相反,使用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();
}

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

 类似资料:
  • 问题内容: 我知道您可以创建目录和文件,但是无法一次性完成这两种操作吗? 即如果文件夹不存在时我要执行以下操作: 错误: 有没有人想出一个功能来解决这个问题? 问题答案: 使用两个命令在一个外壳线面相结合: 注意:以前我建议使用来分隔两个命令,但是正如@trysis所指出的,在大多数情况下使用它可能更好,因为万一失败也不会执行。(否则,这可能会导致您可能没有想到的问题。)

  • 这是另一个: 另外,我在其他片段中使uri公共静态化,以便在MainActivity中可以访问,下面是将捕获的图像加载到ImageView的代码:

  • 问题内容: 我只想从完整路径到文件获取文件夹路径。 例如,我想要得到(不包括)。 我已经尝试过这样的事情: 但是它给了我这样的结果: 这不是我需要的结果(为)。 关于如何获取文件路径的任何想法? 问题答案: 您几乎可以使用该功能了。您只需要加入字符串,如下所示。 虽然,我建议使用该函数来执行此操作,但是您只需要传递字符串即可,它将为您完成工作。由于您似乎在Windows上,因此也考虑使用该功能。一

  • 问题内容: 我在具有基本HTTP服务器(没有Express或Connect或类似的东西)的Node服务器上使用Socket.IO。默认情况下,Socket.IO将客户端文件提供给 /socket.io/socket.io.js 我希望能够将该基本路径更改为其他内容,例如 /foo/bar/socket.io/socket.io.js 是否有任何内置方法来执行此操作,或者是否有任何不更改Socket

  • 我正在创建一个应用程序,从画廊上传选定的图像,并上传到一个网络服务。web服务需要所选图像的文件名以及文件内容的base64编码。我已经设法用硬编码的文件路径实现了这一点。然而,我很难获得图像的真实文件路径。我在网上看了很多资料,找到了这段代码,但它对我不起作用: 我得到这个错误: 编辑 忘了提到我正在使用Kitkat。看起来我的问题与KitKat有关。我发现了这个(见下文),它帮助我让我的应用程

  • 我有两个app:app1和app2。 App2具有: paths.xml: App2在其活动中从App1接收获取映像URI的请求。确定URI后,App2活动执行以下操作: 从App2接收回结果后,App1执行以下操作: 在App1中,从App2返回时,我得到以下异常: java.lang.SecurityException:权限拒绝:从ProcessRecord{52a99eb0 3493:com