我想在我的项目中实现以下目标:
Gallery
。Image
或File
,并将其路径输入我的edittext。谁能告诉我怎么做?
答案 0 :(得分:0)
在onActivityResult
上
Uri uri = intent.getData();
然后以这种方式抓住uri路径
MyEditText.setText(uri.getPath());
答案 1 :(得分:0)
试试这个,
对于开放式图库,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
使用此方法获取图像路径,
@SuppressWarnings("unchecked")
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case PICK_IMAGE:
String imagepath = (getAbsolutePath(data.getData()) + "|").split("\\|");
break;
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}