android 4.4以上從相冊進入之後,獲取圖片的路徑的方法

  private String userUri=null;

 //從相冊中選擇照片
    public static final int CHOOSE_PHOTO = 2;
    private void openAlbum() {
        //action_get_content是通過intent中設置的type屬性來判斷具體調用哪個程序的
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, CHOOSE_PHOTO);
    }

    @TargetApi(19)
    private String  handleImageOnKitKat(Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        Log.d(TAG, "handleImageOnKitKat:獲取的 uri=data.getData()  "+uri);
        if (DocumentsContract.isDocumentUri(this, uri)) {
            //如果是document 類型的Uri ,則通過document_id 來處理
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                String id = docId.split(":")[1];//解析出數據格式的ID

                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
                Log.d(TAG, "com.android.providers.media.documents: ");
            } 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);
                Log.d(TAG, "com.android.providers.downloads.documents: ");
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            //如果是普通類型的Uri,則使用普通的方式來處理
            imagePath = getImagePath(uri, null);
//            imagePath = getRealPathFromURI(uri);
            Log.d(TAG, "content: ");
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            //如果是file類型的uri,直接獲取圖片路徑就可以了
            imagePath = uri.getPath();
            Log.d(TAG, "file ");
        }
        Log.d(TAG, "handleImageOnKitKat:1 圖片真實路徑imagePath " + imagePath);
        return imagePath;
    }

    //android 4.4一下就直接調用這個方法就可以了
    private String handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri, null);
        Log.d(TAG, "handleImageBeforeKitKat:2 圖片真實路徑imagePath " + imagePath);
        return imagePath;
    }


    //根據uri獲取路徑
    private String getImagePath(Uri uri, String selection) {
        String path = null;
        //通過Uri 和selection來獲取真實的圖片路徑
        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;
    }

    private String getRealPathFromURI( Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String path = cursor.getString(column_index);

            return path;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

            case CHOOSE_PHOTO:
                if (resultCode == RESULT_OK) {
                    if (Build.VERSION.SDK_INT >= 19) {
                        userUri=handleImageOnKitKat(data);
                    } else {
                        userUri=handleImageBeforeKitKat(data);
                    }
                }

                break;
        }
    }

注意:android 6.0要添加sd卡的動態權限




































 
發佈了23 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章