码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-11-06 14:43:47      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

 

打开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文档图片选择

标签:

原文地址:http://www.cnblogs.com/gaofengworking/p/4941832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!