[英]Retrieve the path of Image from gallery into Edittext
I want to Achieve the following in my project: 我想在我的项目中实现以下目标:
Gallery
on clicking an Edittext. 单击“编辑文本”打开“ Gallery
”。 Image
or File
in the gallery and get its path into my edittext. 在图库中选择一个Image
或File
,然后将其路径放入我的edittext中。 Can anyone tell me how to do this? 谁能告诉我该怎么做?
On your onActivityResult
在您的onActivityResult
Uri uri = intent.getData();
And then grap that uri path this way MyEditText.setText(uri.getPath());
然后通过这种方式来MyEditText.setText(uri.getPath());
该uri路径MyEditText.setText(uri.getPath());
try this, 尝试这个,
For open gallary, 对于开放式画廊
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
use this method to get image path, 使用这种方法来获取图像路径,
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.