Android打开系统的Document文档图片选择

时间:2022-08-03 19:46:43

打开Document UI 过滤图片

    private void startAcitivty() {
Intent intent = new Intent();
intent.setAction("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent,1001);
}
    @Override
protected void onActivityResult(int req, int res, Intent intent) {
Log.d("onActivityResult","1:" + req);
Log.d("onActivityResult","2:" + res);
Log.d("onActivityResult","3:" + intent);
if (intent != null) {
String data = intent.getDataString();
try {
Log.d("onActivityResult","4:" + URLDecoder.decode(data, "UTF-8"));
queryImage(URLDecoder.decode(data, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} }

能够得到图片content uri

下面处理图片数据 得到图片PATH

    private void queryImage(String data) {
String _path="_data";
String _album="bucket_display_name";
int idx = data.lastIndexOf(":");
String id = null;
if (idx != -1) {
id = data.substring(idx + 1);
}
Log.d("onActivityResult","id:" + id);
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, "_id=" + id, null, null);
if (cursor.moveToFirst()) {
String path=cursor.getString(cursor.getColumnIndex(_path));
String album=cursor.getString(cursor.getColumnIndex(_album));
Log.d("onActivityResult","path:" + path + "," + album); File f = new File(path);
if (f.exists()) {
Bitmap b = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(b);
} }
cursor.close();
}

不同手机的文档选择器界面都有所不同。

START u0 {act=android.intent.action.GET_CONTENT typ=image/* cmp=com.android.documentsui/.DocumentsActivity (has extras)} from pid 11910

Android打开系统的Document文档图片选择