Android相机意图无法在某些设备上运行

时间:2014-02-12 13:04:17

标签: android android-camera-intent onactivityresult

在我的应用程序中,有一个按钮可启动摄像头意图并将捕获的图像保存在给定路径中。当用户点击相机上的右标记或保存选项时,保存捕获的图像。同时当用户点击相机的右标记或保存选项时,相机意图启动器活动的onActivityResult()被调用。这一切都正常,但是在某些设备上,相机意图会在按钮单击时启动,但是当用户在捕获图像后单击保存按钮时,相机将不会关闭,也不会返回onActivityResult()。为了启动相机,我正在使用这个意图。

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

请帮助解决此问题...我感谢您的宝贵答案。谢谢提前

1 个答案:

答案 0 :(得分:1)

使用此代码:

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

在你的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");           
             }