0

I have written the code for opening the images in Gallery view.I want to see the full size of the image when clicked on perticular image.Now I am able to see all the images but when clicked on perticular Image getting exception.

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);
    }

}

StackTrace

 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)
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
user1758835
  • 215
  • 4
  • 17
  • Please paste the full exception stack trace, one line is not enough to understand what happens. – Egor Oct 30 '12 at 12:01

4 Answers4

0

I think you should do cursor.moveToFirst(); before you close the cursor.

Also, you can try move the setImage related method to your onActivityResult

dumbfingers
  • 7,001
  • 5
  • 54
  • 80
0

Try calling cursor.moveToFirst(); right after you execute a query like this:

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

You are probably getting a CursorIndexOutOfBoundsException since your cursor is pointing to the -1 position and you are trying to access it's contents without moving it to the first position.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

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

This is because MediaStore.Images.Media.DATA is not the name of a column in cursor, otherwise....getColumnIndexOrThrow must be a integer >-1.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Koy
  • 1
0

Here is the link which solved my problem http://viralpatel.net/blogs/pick-image-from-galary-android-app/

user1758835
  • 215
  • 4
  • 17