1

私(初心者のアンドロイド)は、ユーザーがギャラリーから選択した画像の実際のパス(例:image_name.jpgなど)を取得しようとしています。

コード スニペットは次のとおりです。

 if (requestCode == SELECT_FILE) {
        Uri selectedImage = data.getData();
        Bitmap bitmapImage;
        try {
              bitmapImage =decodeBitmap(selectedImage );
              photoImage = (ImageView) findViewById(R.id.photoImage);
              photoImage.setImageBitmap(bitmapImage);
              photoName = (TextView) findViewById(R.id.designPhoto);
              File thePath = new File(getRealPathFromURI(selectedImage));
              photoName.setText(getRealPathFromURI(selectedImage));
        } catch (Exception e) {
                e.printStackTrace();
        }
 }


 private String getRealPathFromURI(Uri contentURI) {

    String thePath = "no-path-found";
    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
    if(cursor.moveToFirst()){
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        thePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return  thePath;
}

しかし、私はphotoNametextView で何も取得していません。また、多くのコードとガイドを試しましたが、成功しませんでした。

しかし、私がそれを試したとき

photoName.setText(selectedImage.getPath()); 

私は得ています

/document/image:n (where n=any numbers) ex:/document/image:147 

しかし、適切なファイル名またはパスが必要です ex:image_name.jpgまたは/path/image_name.png

過去3時間から試してみて、誰かが私を助けてくれるといいのですが. よろしくお願いします。

4

1 に答える 1

4

私はそれを使って動作させました

MediaStore.Images.Media.DISPLAY_NAME

更新された動作中のコードは、他の人を助けるかもしれません:

private String getRealPathFromURI(Uri contentURI) {

    String thePath = "no-path-found";
    String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME};
    Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
    if(cursor.moveToFirst()){
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        thePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return  thePath;
}
于 2016-03-31T14:48:10.973 に答える