如何在Android中用相机保存临时图片? [英] How can I save temporary pictures with camera in Android?

查看:768
本文介绍了如何在Android中用相机保存临时图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试从相机拍摄照片并将其保存在应用程序的缓存文件夹时,我没有看到可见的结果。
应用程序不会崩溃,但在LogCat我得到这个消息一旦我尝试设置ImageView src字段到刚刚采取的文件的URI:


  09-17 14:03:32.200:I / System.out(5104):不是DRM文件,开头是
09-17 14 :03:32.200:I / System.out(5104):返回的缓冲区
09-17 14:03:32.205:D / skia(5104):--- SkImageDecoder :: Factory返回null
09 -17 14:03:32.205:I / System.out(5104):resolveUri在错误的位图上失败uri:/data/data/com.example.me/cache/prefix1708798995.jpg


代码实际上很简单:

  File tempFile; 

try {
tempFile = File.createTempFile(prefix,.jpg);
} catch(IOException e){
e.printStackTrace();
AppUtil.showErrorToUser(getApplicationContext(),getResources()。getString(R.string.ERROR_upload));
return;
}

mCameraImage = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,CAMERA_REQUEST_CODE);

并且,在 onActivityResult()

  mIvAttach.setImageURI(Uri.parse(mBodyLocalUri)); 

我也非常非常失望,因为我不能访问 / data / data / myapp 文件夹(我的GS3没有根,我仍然关心我的保修),因此我

解决方案

第三方相机应用程序没有权限写入您的内部存储设备,包括 getCacheDir()(这不知何故 createTempFile()正在使用,虽然不清楚我怎么做)。


我也很失望,我无法验证图片是否实际拍摄,因为我无法访问/数据/数据/ myapp文件夹(我的GS3没有根,我仍然关心我的保修),因此我不能告诉问题的起源是在哪里。


首先,您可以使用 adb run-as 。第二,这是同样问题的另一个表现:正如DDMS不能访问内部存储,也不能第三方应用程序。



使用 getExternalCacheDir (),以获取外部存储上的缓存位置,第三方照相机应用程序可以尝试写入。请注意,由于这些应用程式的错误/限制,并非所有第三方相机应用程式都必须写入您要求的位置。


While trying to take a picture from the camera, and saving it on the cache folder of the app, I get no visible result. App doesn't crash, but on the LogCat I got this message once I try to set an ImageView src field to the URI of the file just taken:

09-17 14:03:32.200: I/System.out(5104): Not a DRM File, opening notmally
09-17 14:03:32.200: I/System.out(5104): buffer returned 
09-17 14:03:32.205: D/skia(5104): --- SkImageDecoder::Factory returned null
09-17 14:03:32.205: I/System.out(5104): resolveUri failed on bad bitmap uri: /data/data/com.example.me/cache/prefix1708798995.jpg

Code is quite simple, actually:

File tempFile;

try {
    tempFile = File.createTempFile("prefix", ".jpg");
} catch (IOException e) {
    e.printStackTrace();
    AppUtil.showErrorToUser(getApplicationContext(), getResources().getString(R.string.ERROR_upload));
    return;
}

mCameraImage = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);

And, in the onActivityResult():

mIvAttach.setImageURI(Uri.parse(mBodyLocalUri));

I'm also very disappointed that I can't verify if pictures are actually taken, since I can't access /data/data/myapp folder (my GS3 is not rooted, and I still care about my warranty), therefore I'm not able to tell where the problem's very origin is.

解决方案

Third-party camera apps have no rights to write to your internal storage, including getCacheDir() (which somehow createTempFile() is using, though it's not clear to me exactly how).

I'm also very disappointed that I can't verify if pictures are actually taken, since I can't access /data/data/myapp folder (my GS3 is not rooted, and I still care about my warranty), therefore I'm not able to tell where the problem's very origin is.

First, you can, using adb run-as. Second, this is another manifestation of the same problem: just as DDMS cannot access internal storage, neither can the third-party app.

Use getExternalCacheDir() instead, to get a cache location on external storage, which a third-party camera app can try to write to. Note, though, that not all third-party camera apps will necessarily write to your requested location, due to bugs/limitations in those apps.

这篇关于如何在Android中用相机保存临时图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆