Android 获取手机相册照片,返回并展示

1.添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2.代码的实现

//使用Intent  意图
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, IMAGE);

//Intent  回调
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //获取图片路径
        if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            c.close();

            showImage(imagePath);
        }
    }


//加载图片
    private void showImage(String imaePath) {

        Bitmap bitmap = BitmapFactory.decodeFile(imaePath);
        showSqs.setImageBitmap(bitmap);
    }

3.Android 6.0需要动态申请存储权限,否则不能返回,显示图片

你可能感兴趣的:(android)