0

我已经编写了在图库视图中打开图像的代码。我想在单击特定图像时查看图像的完整大小。现在我可以看到所有图像,但是当单击特定图像时出现异常。

private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;
    ImageView img;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mygrid);
        img=(ImageView)findViewById(R.id.img);
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);

    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
            }
        }
    }
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String filePath = cursor.getString(column_index);
        cursor.close();
        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        img.setImageBitmap(yourSelectedImage);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

}

堆栈跟踪

 10-30 17:27:15.109: E/AndroidRuntime(2085): FATAL EXCEPTION: main
 10-30 17:27:15.109: E/AndroidRuntime(2085): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/155 }} to activity {com.example.babysnap/com.example.babysnap.MyGrid}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.ActivityThread.access$2000(ActivityThread.java:117) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.os.Handler.dispatchMessage(Handler.java:99) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.os.Looper.loop(Looper.java:130)       
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.ActivityThread.main(ActivityThread.java:3687) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at java.lang.reflect.Method.invokeNative(Native Method) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at java.lang.reflect.Method.invoke(Method.java:507) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at dalvik.system.NativeStart.main(Native Method) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at com.example.babysnap.MyGrid.getPath(MyGrid.java:53) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at com.example.babysnap.MyGrid.onActivityResult(MyGrid.java:44) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
 10-30 17:27:15.109: E/AndroidRuntime(2085): atandroid.app.ActivityThread.deliverResults(ActivityThread.java:2532)
4

4 回答 4

0

我认为您应该cursor.moveToFirst();在关闭光标之前执行此操作。

此外,您可以尝试将 setImage 相关方法移至您的onActivityResult

于 2012-10-30T12:04:10.967 回答
0

cursor.moveToFirst();在执行如下查询后立即尝试调用:

 Cursor cursor = managedQuery(uri, projection, null, null, null);
 cursor.moveToFirst();

您可能会得到 a CursorIndexOutOfBoundsException,因为您的光标指向 -1 位置,并且您试图访问它的内容而不将其移动到第一个位置。

于 2012-10-30T12:05:06.023 回答
0

getColumnIndexOrThrow(MediaStore.Images.Media.DATA)=====> -1

这是因为 MediaStore.Images.Media.DATA 不是游标中列的名称,否则....getColumnIndexOrThrow 必须是>-1 的整数。

于 2012-10-30T12:05:38.170 回答
0

这是解决我的问题的链接 http://viralpatel.net/blogs/pick-image-from-galary-android-app/

于 2012-10-30T12:23:35.413 回答