我已经编写了用于在图库视图中打开图像的代码。我想在单击图像时看到图像的完整大小。现在我能够看到所有图像,但是当点击特定图像获得异常时。
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)
答案 0 :(得分:0)
我认为在关闭光标之前应该cursor.moveToFirst();
。
此外,您可以尝试将setImage相关方法移至onActivityResult
答案 1 :(得分:0)
在执行如下查询后立即尝试调用cursor.moveToFirst();
:
Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst();
你可能得到一个CursorIndexOutOfBoundsException
,因为你的光标指向-1位置并且你试图访问它的内容而不将它移动到第一个位置。
答案 2 :(得分:0)
getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
=====> -1
这是因为MediaStore.Images.Media.DATA不是游标中列的名称,否则.... getColumnIndexOrThrow必须是整数> -1。
答案 3 :(得分:0)