从SDCard获取图像

时间:2014-08-21 19:02:05

标签: android

我想在画廊中选择的SD卡上获取图像的路径。 我使用this帖子中的代码,但在从图库中选择图像后,程序关闭。 这是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

您认为问题的原因是什么?

1 个答案:

答案 0 :(得分:1)

这必须是从SD卡获取图像的简单代码..

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