質問

In my app, there is a button which launches the camera intent and saves the captured image at given path. The captured image is saved when user clicks on Right mark or save option from camera.At the same time when user click on Right mark or save option from camera the onActivityResult() of Camera intent launcher activity get invoked.It's all working fine, but On some devices, the camera intent get launched on button click but when user clicks on save button after image is captured, the camera won't get closed and also its not returning to onActivityResult(). To launch the camera, I am using this intent.

String path = Environment.getExternalStorageDirectory().toString();
Log.d("PATH", path);
File myNewFolder = new File(path + "/it/Snapshots/");
myNewFolder.mkdirs();
cameraIntent.putExtra( android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + "/it/Snapshots/"+ic+".jpg")));
startActivityForResult(cameraIntent,1888);

Please help to solve this issue...I appreciate your valuable answers.Thanks in advance

役に立ちましたか?

解決

Use this code :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

    try {
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PICK_FROM_CAMERA);
    }
    catch (ActivityNotFoundException e)
    {
        e.printStackTrace();
    }

In your onActivityResult :

if (intent != null && resultcode == RESULT_OK)
             {             

                   Uri selectedImage = intent.getData();

                   String[] filePathColumn = {MediaStore.Images.Media.DATA};
                   Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                   cursor.moveToFirst();
                   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                   String filePath = cursor.getString(columnIndex);
                   Log.v("log","filePath is : "+filePath);

                   cursor.close();
                   try 
                   {
                        ExifInterface exif = new ExifInterface(filePath);
                         orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                        //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
                        Log.v("log", "ort is "+orientation);

                   } 
                   catch (IOException e)
                   {
                       e.printStackTrace();
                   }

                   if(bmp != null && !bmp.isRecycled())
                   {
                       bmp = null;               
                   }

                   File f = new File(filePath);

                   if (orientation==6)
                   {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(90);
                        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                   }
                   else if (orientation==8)
                   {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(270);
                        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                   }

                   else if (orientation==3)
                   {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(180);
                        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                   }

                   else if (orientation==0)
                   {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(0);
                        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                   }



             }
             else
             {
                 Log.v("log", "Photopicker canceled");           
             }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top