0

我正在创建一个 android 应用程序,其中我有一个浏览按钮供用户使用browse an image from the image gallery。我正在获取选定的图像,但我需要name of the image to be displayed in a 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);

        }
    }
}

有人可以告诉我如何在字符串中获取图像名称以显示它????

提前致谢!!

4

2 回答 2

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 
于 2013-05-09T11:02:15.510 回答
0

试试data.getData().getLastPathSegment()这个应该给你图像名称

于 2013-05-09T10:36:15.413 回答