从Google云端硬盘中挑选图片时Android崩溃

时间:2016-07-27 06:57:31

标签: android android-intent android-gallery

我制作了一个Gallery Pick Intent。

  final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
        activity.startActivityForResult(galleryIntent, requestCode);

我从谷歌驱动器中选择一张图片并返回onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
         if (requestCode == REQUEST_GALLERY_PICK) {
            try {
                setAvatar(ImageUtils.getPath(EditClientActivity.this, data.getData(), IMAGE));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        avatarSizeOriginal = null;
    }
}

这里我从选定的图片中获取路径:

 public static String getPath(Context ctx, Uri uri, String fileType) {
        String mPath = null;
        Cursor mCursor;
        String[] mColumns = {MediaStore.Images.Media.DATA};

        if (uri == null) {
            return null;
        }

        // algorithm to be used for devices starting with KK
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            String mId = uri.getPathSegments().get(uri.getPathSegments().size() - 1);
            String mSelection = "";
            Uri mProvider = null;

            if (mId.contains(":")) {
                final String[] split = mId.split(":");
                mId = split[1];
            }

            if (fileType.equals(FileTypes.IMAGE)) {
                mSelection = MediaStore.Images.Media._ID + "=?";
                mProvider = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            }
            if (fileType.equals(FileTypes.VIDEO)) {
                mSelection = MediaStore.Video.Media._ID + "=?";
                mProvider = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            }
            if (fileType.equals(FileTypes.AUDIO)) {
                mSelection = MediaStore.Audio.Media._ID + "=?";
                mProvider = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            mCursor = ctx.getContentResolver().query(mProvider, mColumns, mSelection, new String[]{mId}, null, null);

        } else {
            mCursor = ctx.getContentResolver().query(uri, mColumns, null, null, null);
        }

        int column_index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        if (mCursor != null && mCursor.moveToFirst()) {
            mPath = mCursor.getString(column_index);
            mCursor.close();
        }
        if (mPath==null) {
            mPath = uri.getPath();
        }
        return mPath;
    }

最后当我尝试使用图像时,我遇到了崩溃:

private void setAvatar(String path) throws IOException {

        avatarSizeOriginal = new File(path);
        initialAvatar = new File(path);}

它表示路径/document/acc=1;doc=231

如何处理此类问题?

0 个答案:

没有答案