0

i thought this problem already asked so many times, but honestly i still stuck. i have follow the solution at this Camera Force Closing issue in Samsung Galaxy S3 version 4.1.1, but my app still force close. here is my code :

Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(intentAmbil,CAMERA_CAPTURE);

and onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode==RESULT_OK)
    {
        if(requestCode==CAMERA_CAPTURE)
        {
            Bitmap bitmap = null;
            picUri = data.getData();
            if(picUri!=null)
            {
                bitmap = (Bitmap) data.getExtras().get("data");
            }
            else
            {
                String[] projection = {
                        MediaStore.Images.Thumbnails.IMAGE_ID,
                        MediaStore.Images.Thumbnails.KIND,
                        MediaStore.Images.Thumbnails.DATA};
                String selection = MediaStore.Images.Thumbnails.KIND + "="+MediaStore.Images.Thumbnails.MICRO_KIND;
                String sort = MediaStore.Images.Thumbnails._ID + "DESC";
                Cursor myCursor =getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection,null, sort);
                long imageId = 01;
                long thumbnailImageId = 01;
                String thumbnailPath = "";
                try
                {
                    myCursor.moveToFirst();
                    imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
                    thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
                    thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
                }
                finally
                {
                    myCursor.close();
                }
                //buat Cursor baru untuk mengambil gambar dengan resolusi lebih besar
                String[] largeFileProjection = {
                        MediaStore.Images.ImageColumns._ID,  
                           MediaStore.Images.ImageColumns.DATA};
                String largeFileSort = MediaStore.Images.ImageColumns._ID+"DESC";
                myCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
                String largeImagepath = "";
                try
                {
                    myCursor.moveToFirst();
                    largeImagepath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
                }
                finally
                {
                    myCursor.close();
                }
                Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
                Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
                picUri = uriLargeImage;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Intent cropIntent= new Intent (this, Crop.class);
            cropIntent.putExtra("data", picUri.toString());
            cropIntent.putExtra("gambar", bitmap);
            cropIntent.putExtra("kode","kamera");
            startActivity(cropIntent);
        }

here is the logcat

        12-06 11:01:31.869: D/AndroidRuntime(13636): Shutting down VM
    12-06 11:01:31.879: W/dalvikvm(13636): threadid=1: thread exiting with uncaught exception (group=0x40b02930)
    12-06 11:01:31.909: E/AndroidRuntime(13636): FATAL EXCEPTION: main
    12-06 11:01:31.909: E/AndroidRuntime(13636): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.cobaandroid/com.example.cobaandroid.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3447)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3490)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread.access$1100(ActivityThread.java:153)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.os.Handler.dispatchMessage(Handler.java:99)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.os.Looper.loop(Looper.java:137)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread.main(ActivityThread.java:5260)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at dalvik.system.NativeStart.main(Native Method)
    12-06 11:01:31.909: E/AndroidRuntime(13636): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at com.example.cobaandroid.MainActivity.onActivityResult(MainActivity.java:153)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.Activity.dispatchActivityResult(Activity.java:5293)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3443)
    12-06 11:01:31.909: E/AndroidRuntime(13636):    ... 11 more

i really need your help guys, thanks...

Community
  • 1
  • 1
bohr
  • 631
  • 2
  • 9
  • 29

1 Answers1

1

The issue is coming because when you are trying to get the image ID in the line

imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));

the image has not yet been added to the gallery, hence there is no image ID associated with it.

The possible solution to this issue is either:

  1. Add a delay of few milliseconds by using Handler so that the image is properly added to gallery and hence has an associated ID.

    Note: This solution assumes that in the delay time, say 1000 milliseconds, the image will be added to the gallery.

  2. Keep checking till the time you have got image ID associated with the image and then proceed forward.

    This approach ensures that you have an image ID before proceeding forward.

Community
  • 1
  • 1
Gaurav Saluja
  • 357
  • 3
  • 11