私(初心者のアンドロイド)は、ユーザーがギャラリーから選択した画像の実際のパス(例: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;
}
しかし、私はphotoName
textView で何も取得していません。また、多くのコードとガイドを試しましたが、成功しませんでした。
しかし、私がそれを試したとき
photoName.setText(selectedImage.getPath());
私は得ています
/document/image:n (where n=any numbers) ex:/document/image:147
しかし、適切なファイル名またはパスが必要です ex:image_name.jpg
または/path/image_name.png
過去3時間から試してみて、誰かが私を助けてくれるといいのですが. よろしくお願いします。