拍摄图像后获取文件从乌里 [英] Get File from Uri after Image Capture

查看:94
本文介绍了拍摄图像后获取文件从乌里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个活动相机中的照片。在我的onActivityResult我能够检索URI,但使用文件构造我不能让这个URI的文件。我的目的是这样的:

 意图cameraintent =新的意向书(android.media.action.IMAGE_CAPTURE);
   档案照片=新的文件(Environment.getExternalStorageDirectory(),Pic.jpg);
   cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(照片));
   imageURICamera = Uri.fromFile(照片);
   startActivityForResult(cameraintent,1);
 

我用onActivityResult全球定义的imageURICamera。我认为这个问题有事情做与开放的我们没有合适的属性作为的的文件构造文档指出:

构造使用指定的URI的路径中的新文件。URI必须是一个绝对的和分层的统一资源标识符与文件格式和非空路径组件,但不确定的权限,查询或片段组成部分。

我的onActivityResult看起来像这样的时刻:

 开放的U = imageURICamera;
 getContentResolver()有NotifyChange(U,空)。
 ContentResolver的CR = getContentResolver();
 位图BM;
 尝试 {
    BM = android.provider.MediaStore.Images.Media.getBitmap(CR,U);
    字符串路径= getRealPathFromURI(U);
    文件f =新的文件(路径);
 

当我尝试查询ContentProvider的,因为我得到以下的logcat输出路径:

  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):致命异常:主要
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1,结果= -1,数据= NULL}到活动{tod.dosetracker /tod.dosetracker.ImageViewerActivity}:显示java.lang.NullPointerException
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread.access $ 2000(ActivityThread.java:117)
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:965)
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.os.Handler.dispatchMessage(Handler.java:99)
   07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.os.Looper.loop(Looper.java:123)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread.main(ActivityThread.java:3691)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在java.lang.reflect.Method.invokeNative(本机方法)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在java.lang.reflect.Method.invoke(Method.java:507)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:847)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在dalvik.system.NativeStart.main(本机方法)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):由:显示java.lang.NullPointerException
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在tod.dosetracker.ImageViewerActivity.getRealPathFromURI(ImageViewerActivity.java:315)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在tod.dosetracker.ImageViewerActivity.onActivityResult(ImageViewerActivity.java:251)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.Activity.dispatchActivityResult(Activity.java:3934)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
  07-25 20:04:04.310:ERROR / AndroidRuntime(16438):11 ...更多
  07-25 20:04:04.315:ERROR /(2696):Dumpstate> /数据/日志/ dumpstate_app_error
 

解决方案

您已经指定该文件应保存在code到pic.jpg

 文件照片=新的文件(Environment.getExternalStorageDirectory(),Pic.jpg);
 

您不需要做任何事情更多。您应该能够直接使用它,而不保存在活动结果的回调。

您可以改善你的意图推出code有时间戳的形象。

  SimpleDateFormat的日期格式=新的SimpleDateFormat(YYYYMMDD-HHMMSS);
字符串文件名= dateFormat.format(新的Date())+.JPG;

//或使用时间戳比如字符串文件名= System.currentTimeMillis的()+JPG;

档案照片=新的文件(Environment.getExternalStorageDirectory(),文件名);

意图cameraintent =新的意向书(android.media.action.IMAGE_CAPTURE);
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(照片));
startActivityForResult(cameraintent,1);
 

I need to take a photo with the camera using an activity. In my onActivityResult I am able to retrieve an uri, but I cannot get the file of this uri by using the File constructor. My intent looks like this:

   Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
   File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
   cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
   imageURICamera = Uri.fromFile(photo);
   startActivityForResult(cameraintent, 1);

I use the global defined "imageURICamera" in onActivityResult. I think that the problem has something to do with the Uri not having the proper attributes as the documentation for the file constructor states that:

"Constructs a new File using the path of the specified URI. uri needs to be an absolute and hierarchical Unified Resource Identifier with file scheme and non-empty path component, but with undefined authority, query or fragment components"

My onActivityResult looks like this at the moment:

 Uri u = imageURICamera;
 getContentResolver().notifyChange(u, null);
 ContentResolver cr = getContentResolver();
 Bitmap bm;
 try {
    bm = android.provider.MediaStore.Images.Media.getBitmap(cr, u);
    String path = getRealPathFromURI(u);
    File f = new File(path);

When i try to query the contentProvider for the path i get the following logcat output:

   07-25 20:04:04.310: ERROR/AndroidRuntime(16438): FATAL EXCEPTION: main
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {tod.dosetracker/tod.dosetracker.ImageViewerActivity}: java.lang.NullPointerException
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at  android.os.Handler.dispatchMessage(Handler.java:99)
   07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at       android.os.Looper.loop(Looper.java:123)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread.main(ActivityThread.java:3691)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at java.lang.reflect.Method.invoke(Method.java:507)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at dalvik.system.NativeStart.main(Native Method)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438): Caused by: java.lang.NullPointerException
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at tod.dosetracker.ImageViewerActivity.getRealPathFromURI(ImageViewerActivity.java:315)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at tod.dosetracker.ImageViewerActivity.onActivityResult(ImageViewerActivity.java:251)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at  android.app.Activity.dispatchActivityResult(Activity.java:3934)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
  07-25 20:04:04.310: ERROR/AndroidRuntime(16438):     ... 11 more
  07-25 20:04:04.315: ERROR/(2696): Dumpstate > /data/log/dumpstate_app_error

解决方案

You already have specified this file should be saved to pic.jpg in your code

File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");

You don't need to do anything more. You should be able to use it directly without saving in the activity result callback.

You can improve your intent launch code with a timestamp for the image.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
String fileName = dateFormat.format(new Date()) + ".jpg";

// or use timestamp e.g String fileName = System.currentTimeMillis()+".jpg";

File photo = new File(Environment.getExternalStorageDirectory(), fileName);

Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(cameraintent, 1);

这篇关于拍摄图像后获取文件从乌里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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