从三星手机中的摄像头意图中获取Uri Null

时间:2012-11-17 12:48:07

标签: android android-camera android-camera-intent

我在三星手机的内容中获取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;
                }
            }

3 个答案:

答案 0 :(得分:3)

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

  

从相机捕获照片后,我使用Cursor实现逻辑并迭代光标以获取来自相机的最后拍摄照片的路径。以下代码适用于任何设备。

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

答案 1 :(得分:1)

您好我也面临这个问题,就像我正在检查MOTO G上的应用程序它无法正常工作,但在三星设备上工作所以我做以下编码请检查: -

Uri selectedImageUri = data.getData();

                try {
                    selectedImagePath = getPathBelowOs(selectedImageUri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (selectedImagePath == null) {
                    try {
                        selectedImagePath = getPathUpperOs(selectedImageUri);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


public String getPathBelowOs(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

/**
 * Getting image from Uri
 * 
 * @param contentUri
 * @return
 */
public String getPathUpperOs(Uri contentUri) {// Will return "image:x*"
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
            new String[] { id }, null);

    String filePath = "";

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

答案 2 :(得分:0)

当我们从Android中的相机捕获图像时,Uridata.getdata()为空。我们有两个解决方案来解决这个问题。

  1. 我们可以从Bitmap Image获得Uri路径
  2. 我们可以从光标获得U​​ri路径。
  3. 我将在这里实施所有方法,请仔细观察并阅读: -

    首先,我将介绍如何从位图图像中获取URI: 完整的代码是:

    首先我们将通过Intent捕获图像,这两个方法都是相同的,所以这段代码我只会在这里写一次:

     // Capture Image
            captureImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (intent.resolveActivity(getPackageManager()) != null) {
                        startActivityForResult(intent, reqcode);
                    }
    
                }
            });
    

    现在我们将实现OnActivityResult: - (对于以上两种方法,这将是相同的): -

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
    
            if(requestCode==reqcode && resultCode==RESULT_OK)
            {
    
    Bitmap photo = (Bitmap) data.getExtras().get("data");
    ImageView.setImageBitmap(photo);
    
                // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
                Uri tempUri = getImageUri(getApplicationContext(), photo);
    
                \\ Show Uri path based on Image
                Toast.makeText(LiveImage.this,"Here "+ tempUri, Toast.LENGTH_LONG).show();
    
               \\ Show Uri path based on Cursor Content Resolver
                Toast.makeText(this, "Real path for URI : "+getRealPathFromURI(tempUri), Toast.LENGTH_SHORT).show();
    }
            else
            {
                Toast.makeText(this, "Failed To Capture Image", Toast.LENGTH_SHORT).show();
            }
        }
    

    \现在我们将创建以上所有方法,通过类:

    从Image和Cursor方法创建Uri

    现在来自位图图像的URI路径

      private Uri getImageUri(Context applicationContext, Bitmap photo) {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(LiveImage.this.getContentResolver(), photo, "Title", null);
            return Uri.parse(path);
        }
    

    \ Uri来自保存图像的真实路径

      public String getRealPathFromURI(Uri uri) {
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(idx);
        }