8

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
Peshal
  • 1,508
  • 1
  • 12
  • 22
xuandl
  • 183
  • 1
  • 2
  • 14
  • Check this tutorial, It looks like this is exactly what you are looking for. http://blog.vogella.com/2011/09/13/android-how-to-get-an-image-via-an-intent/ – Peshal Aug 13 '13 at 22:51
  • @Peshal What I am trying to do is to let the user pick an image (previosly taken with the same app) in order to get the Uri. I will use the uri latter for some image processing with openCV – xuandl Aug 14 '13 at 19:40
  • BTW, there is also the option of ACTION_OPEN_DOCUMENT : https://developer.android.com/guide/topics/providers/document-provider#client – android developer May 27 '19 at 06:41

2 Answers2

9

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.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.

PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • How can I suggest the folder where the images of my app were saved? (BTW Thanks for your answer) – xuandl Aug 13 '13 at 22:56
  • 4
    Currently android has no API to specify a folder/album where to select the images. So if you want to let the user know where they were saved you will do it the usual way with a toast message, and if you really need to allow the user select from your album then the other way is to create your own gallery activity, which in fact is not so hard to do. Search in github.com, there sure you will find already some – PerracoLabs Aug 13 '13 at 23:16
  • What I am trying to do is to let the user pick an image (previosly taken with the same app) in order to get the Uri for my app. I will use the uri latter for some image processing with openCV. – xuandl Aug 14 '13 at 19:58
  • 1
    Then create a popupwindow whith a ListiVew and a DataAdapter. The listview should contain an ImageView, and the DataAdapter should use the next cursor example (modify the path part) where you can define your own path and filter: Cursor cursor = managedQuery (android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,Images.Media.DATA + " like '/mnt/sdcard/this_is_an_example/your_path_%'", null, null); – PerracoLabs Aug 15 '13 at 11:03
  • 1
    Why using Intent.ACTION_PICK when you are setting action to ACTION_GET_CONTENT. This is totally misleading! – Shivam Pokhriyal Dec 18 '18 at 13:51
  • dude, the argument for the Intent constructor is the action, you're just resetting it to the equivalent of `new Intent(Intent.ACTION_GET_CONTENT)` – kassim Dec 22 '18 at 02:24
1
public class MainActivity extends AppCompatActivity {
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView= findViewById(R.id.image);
}

public void openImage(View view) {
    Intent intent= new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(intent,100);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==100&&resultCode==RESULT_OK){
        Uri uri=data.getData();
        imageView.setImageURI(uri);
    }
}

}

  • 3
    Please try to provide some explanation to your answer and how and why it solves the question asked. A simple code dump is often not enough. – Gregor Koukkoullis Jul 24 '18 at 21:41