Android4.4 Uri获取图片及document理解

1. DocumentsContract.Document 定义

API文档声明:

Constants related to a document, including Cursor column names and flags. 
严格意义上的document包含指针(或叫游标),纵列和标志。

A document can be either an openable stream (with a specific MIME type), or a directory containing additional documents (with the MIME_TYPE_DIR MIME type). A directory represents the top of a subtree containing zero or more documents, which can recursively contain even more documents and directories. 
一个document能是一个可以操作的流(是特殊MIME类型的)或者磁盘分支(包含可添加的documents<感觉有点难翻译,不好理解>). 一个磁盘分支代表包含0个或多个document的树的顶端,能够包含更多的磁盘分支或document.(似乎是树的存储概念,数据结构-树)。

All columns are read-only to client applications. 
所有的纵列只能被客户端程序所读取。


2. Android4.4 Uri

这里先看下4.4之前的uri的形式:

Uri : content://media/extenral/images/media/17766

再看4.4及以后的Uri形式:

content://com.android.providers.media.documents/document/image%3A82482

两者是不同的,4.4以上的系统使用了document封装过了。 
查看API可以找到关于操作document的类: 
类 DocumentsContract 
API文档说明:

Defines the contract between a documents provider and the platform. 
定义用于连接document provider和平台。

To create a document provider, extend DocumentsProvider, which provides a foundational implementation of this contract.

All client apps must hold a valid URI permission grant to access documents, typically issued when a user makes a selection through ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT, or ACTION_OPEN_DOCUMENT_TREE.

详见Java.lang.Object 
↳ android.provider.DocumentsContract

该类提供许多关于操作document的方法。


3. 获取图片代码分析document

获取图片代码

private void handleImageOkKitKat(Intent data) {
        String imagePath=null;
        Uri uri = data.getData();
        Log.d("intent.getData :",""+uri);
        if (DocumentsContract.isDocumentUri(this,uri)){
            String docId = DocumentsContract.getDocumentId(uri);
            Log.d("getDocumentId(uri) :",""+docId);
            Log.d("uri.getAuthority() :",""+uri.getAuthority());
            if ("com.android.providers.media.documents".equals(uri.getAuthority())){
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            }
            else if ("com.android,providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
            }

        }
        else if ("content".equalsIgnoreCase(uri.getScheme())){
            imagePath = getImagePath(uri,null);
        }
        displayImage(imagePath);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

4.4以上系统获得的Uri内容:

Uri uri = data.getData();
Log.d("uri=intent.getData :",""+uri);
  • 1
  • 2
  • 1
  • 2

运行结果:D/uri=intent.getData :﹕ content://com.android.providers.media.documents/document/image%3A82483 
可以看到Uri不同于之前系统版本,选取不同的图片观察Uri是相同的。

  • 判断该Uri是否是document封装过的 
    静态方法:isDocumentUri(Context context, Uri uri) 
    DocumentsContract.isDocumentUri(this,uri);

  • 获取图片数据库数据表里指定的行getDocumentId(Uri uri) 
    静态方法:getDocumentId(Uri uri)

    DocumentsContract.getDocumentId(uri);
    Log.d("getDocumentId(uri) :",""+docId);
  • 1
  • 2
  • 1
  • 2

运行结果: 
D/getDocumentId(uri) :﹕ image:82482 
返回一个字符串,实际上就代表了该图片存储数据库数据表里的行的位置。

  • 获取Uri的路径 getAuthority() 
    属于Uri对象的对象方法。
    uri.getAuthority();
    Log.d("uri.getAuthority() :",""+uri.getAuthority());
  • 1
  • 2
  • 1
  • 2

运行结果: 
D/uri.getAuthority() :﹕ com.android.providers.media.documents 
可以对照Uri的组成判断。 
“content://” + 数据的路径 + 标示ID(可选)

如果路径不同获取图片是不一样的: 
代码:

 if ("com.android.providers.media.documents".equals(uri.getAuthority())){
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            }
            else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

个人理解是com.android.providers.media.documents路径下的图片是经过特殊封装的,而com.android.providers.downloads.documents并没有,这里再观察获取图片完整路径的方法的代码:

public String getImagePath(Uri uri,String selection) {
        String path = null;
        Cursor cursor = getContentResolver().query(uri,null,selection,null,null);   //内容提供器
        if (cursor!=null){
            if (cursor.moveToFirst()){
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));   //获取路径
            }
        }
        cursor.close();
        return path;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

本质是使用内容提供器获取数据。 
观察com.android.providers.media.documents路径和com.android.providers.downloads.documents获取完整路径的代码

可以知道com.android.providers.media.documents使用document封装是向数据表添加了MediaStore.Images.Media.DATA列形成了一个新数据库

MediaStore.Images.Media.EXTERNAL_CONTENT_URI只有根据MediaStore.Images.Media.DATA来查询,此时的id(id指的不是之前的getDocumentId(uri)的docID,而是将docID以:分割获取的数值标号)是对应图片行MediaStore.Images.Media.DATA列的标号。

com.android.providers.downloads.documents路径则是以4.4之前版本的形式的Uri存储的。

docID: image:82482 
id: 82482 
以’:’分割字符串,获取id


Android4.4 之前获取图片路径

private void handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri,null);
        displayImage(imagePath);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

Uri里就包含了该图片所在行的id。

Uri : content://media/extenral/images/media/17766

使用内容提供器可以轻松获取路径。


4. DEMO 主要代码

public class MainActivity extends AppCompatActivity {


    private static final int TAKE_PHOTO = 1;
    private static final int CROP_PHOTO = 2;
    private static final int CHOOSE_PIC = 3;
    private Button takePhoto;
    private Button choosePicture;
    private ImageView picture;
    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        picture = (ImageView) findViewById(R.id.picture);
        takePhoto = (Button) findViewById(R.id.take_photo);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File outputFile = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
                try{
                    if (outputFile.exists()){
                        outputFile.delete();
                    }
                    outputFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(outputFile);
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
        choosePicture = (Button) findViewById(R.id.choose_picture);
        choosePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                startActivityForResult(intent,CHOOSE_PIC);
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode==RESULT_OK){
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(imageUri,"image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                    startActivityForResult(intent,CROP_PHOTO);
                }
                break;
            case CROP_PHOTO:
                if (resultCode==RESULT_OK){
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case CHOOSE_PIC:
                if (resultCode==RESULT_OK){
                    if (Build.VERSION.SDK_INT>=19){
                        handleImageOkKitKat(data);
                    }
                    else{
                        handleImageBeforeKitKat(data);
                    }
                }
            default:
                break;
        }
    }

    private void handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri,null);
        displayImage(imagePath);
    }

    private void handleImageOkKitKat(Intent data) {
        String imagePath=null;
        Uri uri = data.getData();
        Log.d("uri=intent.getData :",""+uri);
        if (DocumentsContract.isDocumentUri(this,uri)){
            String docId = DocumentsContract.getDocumentId(uri);        //数据表里指定的行
            Log.d("getDocumentId(uri) :",""+docId);
            Log.d("uri.getAuthority() :",""+uri.getAuthority());
            if ("com.android.providers.media.documents".equals(uri.getAuthority())){
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
            }
            else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
                imagePath = getImagePath(contentUri,null);
            }

        }
        else if ("content".equalsIgnoreCase(uri.getScheme())){
            imagePath = getImagePath(uri,null);
        }
        displayImage(imagePath);
    }

    private void displayImage(String imagePath) {
        if (imagePath!=null){
            Bitmap bitImage = BitmapFactory.decodeFile(imagePath);
            picture.setImageBitmap(bitImage);
        }
        else{
            Toast.makeText(MainActivity.this,"failed to get image",Toast.LENGTH_SHORT).show();
        }
    }

    public String getImagePath(Uri uri,String selection) {
        String path = null;
        Cursor cursor = getContentResolver().query(uri,null,selection,null,null);   //内容提供器
        if (cursor!=null){
            if (cursor.moveToFirst()){
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));   //获取路径
            }
        }
        cursor.close();
        return path;
    }
}
原文http://blog.csdn.net/svizzera/article/details/50705195

你可能感兴趣的:(Android)