Android 相机 Intent 在某些设备上不起作用

标签 android android-camera-intent onactivityresult

在我的应用程序中,有一个按钮可以启动相机 Intent 并将捕获的图像保存在给定路径。当用户单击右侧标记或相机中的保存选项时,将保存捕获的图像。同时,当用户单击右侧标记或相机中的保存选项时,将调用相机 Intent 启动器 Activity 的 onActivityResult() 。一切正常,但是在某些设备上,相机 Intent 在单击按钮时启动,但当用户在捕获图像后单击保存按钮时,相机不会关闭,也不会返回到 onActivityResult()。为了启动相机,我使用了这个 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);

请帮助解决这个问题...非常感谢您的宝贵回答。提前致谢

最佳答案

使用此代码:

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

关于Android 相机 Intent 在某些设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21728781/

相关文章:

android - 如何检查蓝牙扫描引起的电池消耗

android - 来自对话框 fragment 的 startActivityForResult

java - 从 .class 或类似的东西实例化 Fragment 对象?

android - Cordova /Phonegap - Android : Icon not updating

java - 拍照并命名

android - android 的默认图库和相机 Intent 是否适用于所有设备?

Android:BroadcastReceiver Intent 检测拍摄的相机照片?

android - 从嵌套 fragment 登录 Facebook 无效 - Activity 结果 fragment 索引超出范围

android - Facebook Account Kit OnActivityResult 没有得到正确的数据

java - 在firebase数据获取方法中创建无限循环