打开使用Intent.ACTION_PICK图像 [英] opening an image using Intent.ACTION_PICK

查看:136
本文介绍了打开使用Intent.ACTION_PICK图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共无效button_load_image(查看视图){
    字符串路径= Environment.getExternalStorageDirectory()
            .getAbsolutePath()+/+ SimpleCamera.getAlbumName();
    文件f =新的文件(路径);    如果(f.exists()){
        Log.d(button_load_image,文件夹存在);
        如果(f.isDirectory()){
            Log.d(button_load_image,是目录);
            乌里U = Uri.fromFile(F);
            意向意图=新意图(Intent.ACTION_PICK,U);
            Log.d(Intent.ACTION_PICK,创建);
            startActivityForResult(意向,REQUEST_ code_LOAD_IMAGE);
        }
    }
}保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    开关(要求code){
    案例REQUEST_ code_LOAD_IMAGE:
        如果(结果code == RESULT_OK){
            乌里imageUri = data.getData();
            Log.d(图像选择的路径,imageUri.getPath());
        }
        打破;
    }
}

日志显示:

  08-13 17:11:37.594:D / libEGL(3265):加载/vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-13 17:11:37.594:D / libEGL(3265):加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-13 17:11:37.602:D / libEGL(3265):加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-13 17:11:37.664:D / OpenGLRenderer(3265):启用调试模式0
08-13 17:11:46.672!:D /专辑= NULL(3265)/存储/模拟/ 0
08-13 17:11:49.914:D / button_load_image(3265):文件夹是否存在
08-13 17:11:49.914:D / button_load_image(3265):是目录
08-13 17:11:49.914:D / Intent.ACTION_PICK(3265):创建
08-13 17:11:49.922:D / AndroidRuntime(3265):关闭VM
08-13 17:11:49.922:W / dalvikvm(3265):主题ID = 1:螺纹未捕获的异常退出(组= 0x413b0930)
08-13 17:11:49.930:E / AndroidRuntime(3265):致命异常:主要
08-13 17:11:49.930:E / AndroidRuntime(3265):java.lang.IllegalStateException:无法执行活动的方法
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.view.View $ 1.onClick(View.java:3597)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.view.View.performClick(View.java:4202)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.view.View $ PerformClick.run(View.java:17340)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.os.Handler.handleCallback(Handler.java:725)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.os.Handler.dispatchMessage(Handler.java:92)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.os.Looper.loop(Looper.java:137)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.app.ActivityThread.main(ActivityThread.java:5039)
08-13 17:11:49.930:E / AndroidRuntime(3265):在java.lang.reflect.Method.invokeNative(本机方法)
08-13 17:11:49.930:E / AndroidRuntime(3265):在java.lang.reflect.Method.invoke(Method.java:511)
08-13 17:11:49.930:E / AndroidRuntime(3265):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
08-13 17:11:49.930:E / AndroidRuntime(3265):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-13 17:11:49.930:E / AndroidRuntime(3265):在dalvik.system.NativeStart.main(本机方法)
08-13 17:11:49.930:E / AndroidRuntime(3265):因:java.lang.reflect.InvocationTargetException
08-13 17:11:49.930:E / AndroidRuntime(3265):在java.lang.reflect.Method.invokeNative(本机方法)
08-13 17:11:49.930:E / AndroidRuntime(3265):在java.lang.reflect.Method.invoke(Method.java:511)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.view.View $ 1.onClick(View.java:3592)
08-13 17:11:49.930:E / AndroidRuntime(3265):11 ...更多
08-13 17:11:49.930:E / AndroidRuntime(3265):android.content.ActivityNotFoundException:致无活动处理意向{行动= android.intent.action.PICK DAT =文件:///存储/模拟/ 0 / simple_pic}
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.app.Activity.startActivityForResult(Activity.java:3370)
08-13 17:11:49.930:E / AndroidRuntime(3265):在android.app.Activity.startActivityForResult(Activity.java:3331)
08-13 17:11:49.930:E / AndroidRuntime(3265):在com.example.mc.MC_Memu.button_load_image(MC_Memu.java:53)
08-13 17:11:49.930:E / AndroidRuntime(3265):14 ...更多


解决方案

ACTION_PICK是允许用户从任何已登记为这种行动安装的应用程序选择一个图像。这是不可能的,从选择哪个相册中指定。它是在用户自行决定使用哪个应用程序,并浏览到想要的专辑选择照片。

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

 意向意图=新意图(Intent.ACTION_PICK);
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(意向,REQUEST_ code_LOAD_IMAGE);

和中的onActivityResult,除了RESULT_OK,还应检查data.getData()!= null,如一个应用程序能够正确(不是取消)关闭而不在所有返回的图像。

I am trying to open an image using intent.ACTION_PICK but when I start the activity using startActivityForResoult my app crashes. Any clues of what I am doing wrong?

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

The log shows:

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 is to allow a user to select an image from any of the installed apps which registered for such an action. It is not possible to specify from which album to select. It is at the user discretion to decide which app to use and to browse to the desired album to select the photo.

So taking out the folder parameter, you can try this:

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

And in the onActivityResult, besides the RESULT_OK, you should also check for data.getData() != null, as an app could close correctly (not cancelling) without returning an image at all.

这篇关于打开使用Intent.ACTION_PICK图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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