2013-05-09 60 views
0

我正在創建一個Android應用程序,其中我有一個用戶瀏覽按鈕到browse an image from the image gallery。 我得到選定的圖像,但我需要在我的活動name of the image to be displayed in a textview我該怎麼做? 我越來越像這樣:如何讓圖像名稱顯示在TextView中?

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
      System.out.println("Image Path : " + selectedImagePath); 
      img.setImageURI(selectedImageUri); 

      upload_row=new TableRow(this); 

      appln_no=new TextView(this); 
      image_name=new TextView(this); 
      doctype=new TextView(this); 

      appln_no.setText("Application no."); 
      image_name.setText(selectedImagePath); 
      doctype.setText("DOCUMENT_TYPE"); 

      upload_row.addView(appln_no); 
      upload_row.addView(image_name); 
      upload_row.addView(doctype); 

      upload_table.addView(upload_row); 

     } 
    } 
} 

有人可以告訴我,我怎麼能在一個字符串獲取圖像名稱以顯示它????

在此先感謝!

+0

嘗試回答以下 – Raghunandan 2013-05-09 11:06:27

回答

4

爲了得到真實路徑使用下面的函數

public String getRealPathFromURI(Uri contentUri) { 
// can post image 
String [] proj={MediaStore.Images.Media.DATA}; 
Cursor cursor = managedQuery(contentUri, 
proj, // Which columns to return 
null, // WHERE clause; which rows to return (all rows) 
null, // WHERE clause selection arguments (none) 
null); // Order-by clause (ascending by name) 
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
cursor.moveToFirst(); 
return cursor.getString(column_index); 

    } 

獲取真實路徑

 Uri selectedImageUri = data.getData(); 
    String s= getRealPathFromURI(selectedImageUri); 

使用子如下

 String name = s.substring(s.lastIndexOf("/") + 1) 
    // instead of "/" you can also use File.sepearator 
    System.out.println("......"+ name); 
    image_name=new TextView(ActivityName.this); 
    image_name.setText(name); 

例子:

String str="/storage/sdcard0/MyFolder/After school children sports day.jpg"; 
    System.out.println("......"+ str.substring(str.lastIndexOf("/") + 1)); 

輸出

......After school children sports day.jpg 
+1

非常感謝!這是我需要的!完善! – shivani 2013-05-09 12:15:43

0

嘗試data.getData().getLastPathSegment()這應該給你的圖像名稱

相關問題