【问题标题】:How should I save an image on Android? [closed]我应该如何在 Android 上保存图像? [关闭]
【发布时间】:2013-04-12 14:23:16
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

标签: android image file caching save


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-16
    • 2011-12-24
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多