相册选择以后根据uri获取图片路径

2018-04-16  本文已影响0人  西决_7e15

Intent intent =new Intent(Intent.ACTION_PICK, null);

intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");

if (getActivity()==null)

return;

startActivityForResult(intent, TakePhotoEnum.CHOICE_ALBUM.getRequestCode());

case 202:

if(resultCode==RESULT_OK){

mListImage.remove(mListImage.size()-1);

        Uri uri = data.getData();

        mListImage.add(new PostImageBean(getDataColumn(uri,null,null)));

        mListImage.add(new PostImageBean(""));

        mBaseRecyclerAdapter.init(mListImage);

        mBaseRecyclerAdapter.notifyDataSetChanged();

    }

break;

/**

* 根据uri获取当前路径

*/

public StringgetDataColumn(Uri uri, String selection, String[] selectionArgs) {

Cursor cursor =null;

    final String column ="_data";//路径保存在downloads表中的_data字段

    final String[] projection = {column};

    try {

cursor = getContext().getContentResolver().query(uri, projection, selection, selectionArgs, null);

        if (cursor !=null && cursor.moveToFirst()) {

final int column_index = cursor.getColumnIndexOrThrow(column);

            return cursor.getString(column_index);

        }

}catch (Exception e) {

e.printStackTrace();

    }finally {

if (cursor !=null)

cursor.close();

    }

return null;

}

上一篇 下一篇

猜你喜欢

热点阅读