Roki20121
Roki20121

Reputation: 115

Getting a image from an SDCard

I want to get the path to an image on sdcard selected in gallery. I use the code from this post, but after selecting the image from gallery the program shuts down. Here is the Logcat :

    E/AndroidRuntime(28203): FATAL EXCEPTION: main
    E/AndroidRuntime(28203): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/25992 }} to activity {com.example.simplewidget/com.example.simplewidget.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    E/AndroidRuntime(28203):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
    E/AndroidRuntime(28203):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
    E/AndroidRuntime(28203):    at android.app.ActivityThread.access$1100(ActivityThread.java:123)
    E/AndroidRuntime(28203):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
    E/AndroidRuntime(28203):    at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(28203):    at android.os.Looper.loop(Looper.java:137)
    E/AndroidRuntime(28203):    at android.app.ActivityThread.main(ActivityThread.java:4424)
    E/AndroidRuntime(28203):    at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(28203):    at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime(28203):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    E/AndroidRuntime(28203):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    E/AndroidRuntime(28203):    at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime(28203): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    E/AndroidRuntime(28203):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
    E/AndroidRuntime(28203):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    E/AndroidRuntime(28203):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    E/AndroidRuntime(28203):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
    E/AndroidRuntime(28203):    at com.example.simplewidget.MainActivity.getPath(MainActivity.java:60)
    E/AndroidRuntime(28203):    at com.example.simplewidget.MainActivity.onActivityResult(MainActivity.java:27)
    E/AndroidRuntime(28203):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
    E/AndroidRuntime(28203):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
    E/AndroidRuntime(28203):    ... 11 more

What do you think is the cause of the problem?

Upvotes: 0

Views: 64

Answers (1)

KD-21
KD-21

Reputation: 255

This must be a simple code to get an Image from a SD Card..

File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);

Upvotes: 1

Related Questions