gpt4 book ai didi

android - 使用 Intent.ACTION_PICK 打开图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:47 27 4
gpt4 key购买 nike

我正在尝试使用 intent.ACTION_PICK 打开图像,但是当我使用 startActivityForResoult 启动 Activity 时,我的应用程序崩溃了。我做错了什么的任何线索?

    public void button_load_image(View view) {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + SimpleCamera.getAlbumName();
File f = new File(path);

if (f.exists()) {
Log.d("button_load_image", "folder exists");
if (f.isDirectory()) {
Log.d("button_load_image", "is directory");
Uri u = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_PICK, u);
Log.d("Intent.ACTION_PICK", "IS CREATED");
startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);
}
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_LOAD_IMAGE:
if (resultCode == RESULT_OK) {
Uri imageUri= data.getData();
Log.d("image selected path", imageUri.getPath());
}
break;
}
}

日志显示:

08-13 17:11:37.594: D/libEGL(3265): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-13 17:11:37.594: D/libEGL(3265): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-13 17:11:37.602: D/libEGL(3265): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-13 17:11:37.664: D/OpenGLRenderer(3265): Enabling debug mode 0
08-13 17:11:46.672: D/album != null(3265): /storage/emulated/0
08-13 17:11:49.914: D/button_load_image(3265): folder exists
08-13 17:11:49.914: D/button_load_image(3265): is directory
08-13 17:11:49.914: D/Intent.ACTION_PICK(3265): IS CREATED
08-13 17:11:49.922: D/AndroidRuntime(3265): Shutting down VM
08-13 17:11:49.922: W/dalvikvm(3265): threadid=1: thread exiting with uncaught exception (group=0x413b0930)
08-13 17:11:49.930: E/AndroidRuntime(3265): FATAL EXCEPTION: main
08-13 17:11:49.930: E/AndroidRuntime(3265): java.lang.IllegalStateException: Could not execute method of the activity
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.view.View$1.onClick(View.java:3597)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.view.View.performClick(View.java:4202)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.view.View$PerformClick.run(View.java:17340)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.os.Handler.handleCallback(Handler.java:725)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.os.Looper.loop(Looper.java:137)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-13 17:11:49.930: E/AndroidRuntime(3265): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 17:11:49.930: E/AndroidRuntime(3265): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 17:11:49.930: E/AndroidRuntime(3265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-13 17:11:49.930: E/AndroidRuntime(3265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-13 17:11:49.930: E/AndroidRuntime(3265): at dalvik.system.NativeStart.main(Native Method)
08-13 17:11:49.930: E/AndroidRuntime(3265): Caused by: java.lang.reflect.InvocationTargetException
08-13 17:11:49.930: E/AndroidRuntime(3265): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 17:11:49.930: E/AndroidRuntime(3265): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.view.View$1.onClick(View.java:3592)
08-13 17:11:49.930: E/AndroidRuntime(3265): ... 11 more
08-13 17:11:49.930: E/AndroidRuntime(3265): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=file:///storage/emulated/0/simple_pic }
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.app.Activity.startActivityForResult(Activity.java:3370)
08-13 17:11:49.930: E/AndroidRuntime(3265): at android.app.Activity.startActivityForResult(Activity.java:3331)
08-13 17:11:49.930: E/AndroidRuntime(3265): at com.example.mc.MC_Memu.button_load_image(MC_Memu.java:53)
08-13 17:11:49.930: E/AndroidRuntime(3265): ... 14 more

最佳答案

ACTION_PICK 允许用户从注册了此类操作的任何已安装应用程序中选择图像。无法指定从哪个专辑中选择。用户可以自行决定使用哪个应用程序并浏览到所需的相册以选择照片。

所以取出文件夹参数,你可以试试这个:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);

在 onActivityResult 中,除了 RESULT_OK 之外,您还应该检查 data.getData() != null,因为应用可以正确关闭(而不是取消)而根本不返回图像。

关于android - 使用 Intent.ACTION_PICK 打开图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18220152/

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