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