gpt4 book ai didi

java - 图像捕获后从 Uri 获取文件

转载 作者:搜寻专家 更新时间:2023-11-01 07:38:45 39 4
gpt4 key购买 nike

我需要使用 Activity 用相机拍照。在我的 onActivityResult 中,我能够检索一个 uri,但我无法使用 File 构造函数获取该 uri 的文件。我的 Intent 是这样的:

   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);

我在 onActivityResult 中使用全局定义的“imageURICamera”。我认为问题与 Uri 没有适当的属性有关 documentation对于文件构造函数声明:

“使用指定 URI 的路径构造一个新文件。uri 需要是一个绝对的、层次化的统一资源标识符,具有文件方案和非空路径组件,但具有未定义的权限、查询或 fragment 组件”

我的 onActivityResult 目前看起来像这样:

 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);

当我尝试向 contentProvider 查询路径时,我得到以下 logcat 输出:

   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

最佳答案

您已经在代码中指定该文件应保存为 pic.jpg

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

您无需再做任何事情。您应该能够直接使用它而无需保存在 Activity 结果回调中。

您可以使用图像的时间戳改进您的 Intent 启动代码。

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);

关于java - 图像捕获后从 Uri 获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819985/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com