2

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

@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;
                }
            }
4

3 回答 3

3

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

从相机拍摄照片后,我使用光标实现了一个逻辑并迭代光标以获取最后一张从相机拍摄的照片的路径。下面的代码在任何设备上都可以正常工作。

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();
}
于 2013-02-23T15:57:49.990 回答
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;
}
于 2014-05-28T20:13:37.900 回答
1

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

  1. 我们可以从 Bitmap Image 中得到 Uri 路径
  2. 我们可以从光标处获取 Uri 路径。

我将在这里实现所有方法,请仔细观看并阅读这些:-

首先我将告诉如何从位图图像中获取 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);
    }
于 2018-06-12T15:53:12.047 回答