通过在android中读取存储来设置ImageButton背景

时间:2016-03-15 16:06:49

标签: android storage imagebutton

我使用以下内容打开存储空间并设置ImageButton的背景。

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        //String[] filePathColumn = {Environment.DIRECTORY_PICTURES};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


        imgLogo.setImageBitmap(BitmapFactory.decodeFile(picturePath));


    }

但即使在明显的权限存在,我也无法设置Imagebutton背景

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

我得到......     需要android.permission.READ_EXTERNAL_STORAGE或grantUriPermission()

1 个答案:

答案 0 :(得分:0)

它对我有用。

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath,bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imgLogo.setImageBitmap(bitmap);

另外,请检查图像视图的可见性。