我正在创建一个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);
}
}
}
有人可以告诉我如何在字符串中显示图像名称以显示它????
提前致谢!!
答案 0 :(得分: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 :(得分:0)
尝试data.getData().getLastPathSegment()
这应该会为您提供图片名称