android - getpath方法返回错误的地址

时间:2013-09-15 04:20:40

标签: android android-intent path gallery syntax-error

我使用startActivityForResult从图库中选择一张图片,然后使用onActivityResult来显示结果。 当我使用getPath()的意图结果将路径发送到另一个活动来设置该图片的来源时,路径不正确,这是图片不在那里的另一种方式。

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

photopicker:

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

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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