-4

我想知道在 Android 上保存图像的最有效方法是什么。

我的应用程序基本上是这样的:你看到一张默认信用卡,接下来你可以选择扫描一个条形码(使用 zxing),然后它会为它生成一个 QR 码,并将 QR 码放在 ImageView 中默认卡已存储。

到目前为止,一切都很好; 但是,应用程序在离开结果(应用程序中的另一个屏幕,转到设备的主屏幕等)并返回屏幕时记住要显示的图像存在问题。它再次显示默认卡。

现在,我知道我必须保存二维码,但我想不出解决方案。该设备没有 SD 卡,因此无法保存到外部存储设备。

我尝试通过 SharedPreferences、OutputStream 和缓存来解决它;但无法让它工作。

您会选择这 3 种方式中的哪一种(或者可能是不同的方式,欢迎所有帮助),代码是什么?

4

1 回答 1

1
public boolean saveBitmap(Bitmap image, String name){
    try {
        FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
        image.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.close();
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public Bitmap getBitmap(String name){
    try {
        FileInputStream fis = context.openFileInput(name);
        Bitmap image = BitmapFactory.decodeStream(fis);
        return image;
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
于 2013-04-19T08:51:15.143 回答