使用相机android捕获图像后应用程序崩溃

时间:2013-12-05 18:18:43

标签: android android-camera-intent

我认为这个问题已经问过很多次了,老实说我还是坚持了。我已按照此Camera Force Closing issue in Samsung Galaxy S3 version 4.1.1的解决方案,但我的应用仍然强行关闭。 这是我的代码:

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

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

这里是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

我真的需要你的帮助,谢谢...

1 个答案:

答案 0 :(得分:1)

问题来了,因为当您尝试在行中获取图像ID时

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

图片尚未添加到图库中,因此没有与之关联的图片ID。

此问题的可能解决方案是:

  1. 使用Handler添加几毫秒的延迟,以便将图像正确添加到图库,因此具有关联的ID。

    注意:此解决方案假设在延迟时间内,例如1000毫秒,图像将被添加到图库中。

  2. 继续检查,直到您获得与图像相关联的图像ID为止,然后继续前进。

    此方法可确保您在继续前进之前拥有图像ID。