2016-03-31 11 views
0

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

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たい:過去3からそれをしようと​​か/path/image_name.png

を誰かが私を助けることができれば素晴らしいだろう。謙虚に感謝します。

+1

は、なぜあなたはそれが「本当のパスを」持っていることを前提としていますか?なぜあなたは 'MediaStore.Images.Media.DISPLAY_NAME'を使わないのですか? – Selvin

+0

私は3時間を無駄にした後、アンドロイドコーディングに新しいので、どこで、なぜ私が間違っているのか質問する必要があります –

+0

@Selvin Sirありがとうございます 'MediaStore.Images.Media.DISPLAY_NAME'はたくさんの魅力的な作品です –

答えて

1

は、私はそれを使用して作業しました

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; 
} 
関連する問題