Android - 从图库加载图像并设置为背景

标签 android image background gallery

我想将图像库中的图像加载到我的应用程序中并将该图像设置为背景。

这适用于以下代码:

Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select background"), SELECT_PICTURE);



public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();

            Drawable bg;
            try {
                InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
                bg = Drawable.createFromStream(inputStream, selectedImageUri.toString());
            } catch (FileNotFoundException e) {
                bg = ContextCompat.getDrawable(this, R.drawable.bg);
            }

            drawView.startNew();
            drawView.setBackground(bg);
        }
    }
}

问题是我想将此信息存储在应用程序中,并且在用户关闭并重新打开应用程序后我仍然希望拥有自定义背景图像。

我不知道如何存档。

我尝试存储图像的 Uri,但遇到了一些权限问题 (android.permission.MANAGE_DOCUMENTS),这是已知问题。

如何得到预期的结果?

最佳答案

onActivityResult中的图像保存到存储中,然后将文件路径保存在SharedPreference或数据库中...因此每次用户回来时,您都会加载它并设置作为背景

关于Android - 从图库加载图像并设置为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522893/

相关文章:

android - jquery mobile 和 phonegap addJavascriptInterface

android studio - 如何制作两个自定义应用程序类

android - 广播接收器不工作

Android RatingBar 显示超过 5 颗星

image - Matlab 使用线性索引对二维进行索引,同时保持第三维不变

css - ie6 修复需要背景。需要一个解决方法

python - 如何在 Python 中从图片的其余部分剪切绿色背景和前景?

IOS:使用延迟加载图像进行 JSON 解析

android - 将从相机拍摄的图像保存到内部存储器

android - 类似 Twitter 的背景动画可在 Android 中绘制