android - 在三星手机中从相机 Intent 捕获照片时获取 Uri Null

标签 android android-camera android-camera-intent

在从相机拍摄照片时,我在三星手机的 contenturi 中得到 null,但其他手机工作正常。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
        super.onActivityResult(requestCode, resultCode, data);    
        try
        {
             if (requestCode == IMAGE_CAPTURE) {
                if (resultCode == RESULT_OK){

                    Uri contentUri = data.getData();
                    if(contentUri!=null)
                    {
                        String[] proj = { MediaStore.Images.Media.DATA };         
                        Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
                        cursor.moveToFirst();         
                        imageUri = Uri.parse(cursor.getString(column_index));
                    }

                    tempBitmap = (Bitmap) data.getExtras().get("data"); 
                    mainImageView.setImageBitmap(tempBitmap);
                    isCaptureFromCamera = true;
                }
            }

最佳答案

上面的代码适用于某些手机,但在我的情况下不适用于三星手机,因此我为所有设备实现了通用逻辑。

After capturing the photo from camera, I implement a logic using Cursor and iterate the cursor to get the path of last captured photo from camera.The below code works fine on any device.

Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
    do {
        uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
        photoPath = uri.toString();
    }while(cursor.moveToNext());
    cursor.close();
}

关于android - 在三星手机中从相机 Intent 捕获照片时获取 Uri Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13430720/

相关文章:

android - 我应该使用 MonoDroid 还是 Android?

java - 类作为 Android 应用程序的弹出窗口

android - 安卓模拟器中的摄像头

android - Camera Intent,FileUriExposedException,将图像保存在存储中并检索图像位图和文件路径

android - 获取适用于 Android Firebase 的 SHA-1

android - Cardview 在低于 Lollipop 的设备中无法正常工作

android - 自 Android 4.0 以来,Camera.lock、unlock() 自动为您管理?

android - 如何修复Android手机相机的帧速率

android - 相机 'OK' 按钮不起作用

android - 三星 Galaxy 设备相机 Intent 问题