我使用startActivityForResult从图库中挑选图片,然后使用onActivityResult带来结果。
当我使用getPath()作为目的结果将路径发送到另一个 Activity 来设置图片的来源时,路径是错误的,这是图片不在那里的另一种方式。

图片位于sdcard中,位于:“mnt/sdcard/pictures/lambo” ----但getpath()他的返回:“external/images/media/17

拾光器:

private void photopicker() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);
}

onActivityResult:
  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                String add;
                add = selectedImage.getPath();   // don't work
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                selectedPhoto = BitmapFactory.decodeStream(imageStream);
//              add = selectedImage.getPath(); //  don't work
                Intent intent = new Intent(MainActivity.this, PicViewer.class);
                intent.putExtra("add", add);
                startActivity(intent);
            }
        }
    }

最佳答案

您可以尝试以下方法:

public String getRealPathFromURI(Uri contentUri) {
    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();
    return cursor.getString(column_index);
}

07-27 18:51