0

I used startActivityForResult to pick a picture from gallery and then used onActivityResult for bring the result. when i use getPath() for the intent result to send the path to another activity to set the source of that picture , the path is incorrect and is another way that the picture is not located there .

the picture is located in sdcard and located at : "mnt/sdcard/pictures/lambo"---- but getpath() his returns : "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);
            }
        }
    }
4

1 回答 1

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);
}
于 2013-09-15T09:03:52.063 回答