我应该如何保存在Android上的图像? [英] How should I save an image on Android?

查看:81
本文介绍了我应该如何保存在Android上的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

什么我的应用程序基本上做到这一点是:你看到一个默认的信用卡,然后你可以选择扫描棒code(带zxing),那么这将产生一个QR- code代表它,放置QR- code在被存储在默认卡ImageView的。

到目前为止好;然而,与应用的记忆(在应用程序中的另一个屏幕,将设备的主屏幕等),从结果中航行时程,回到屏幕来显示其图像的问题。这再次说明默认卡。

现在,我明白我必须救QR- code,但我不能拿出一个解决方案。该器件具有使保存到外部存储无SD卡不是一个选项。

我试着通过共享preferences,OutputStream的,并且缓存解决它;但不能得到它的工作。

其中的那些3种方式(或者一个不同的,所有帮助是值得欢迎的),你会选择,并会在code是什么呢?


解决方案

 公共布尔saveBitmap(位图图像​​,字符串名称){
    尝试{
        FOS的FileOutputStream = context.openFileOutput(姓名,Context.MODE_PRIVATE);
        image.com preSS(Bitmap.Com pressFormat.JPEG,70,FOS);
        fos.close();
        返回true;
    }
    赶上(例外五){
        e.printStackTrace();
        返回false;
    }
}公共位图getBitmap(字符串名称){
    尝试{
        FIS的FileInputStream = context.openFileInput(名);
        位图图像= BitmapFactory.de codeStream(FIS);
        返回形象;
    }
    赶上(例外五){
        e.printStackTrace();
        返回null;
    }
}

I was wondering what the most efficient way of saving an image on Android is.

What my app basically does is this: you see a default credit card, next you can choose to scan a barcode (with zxing), which will then generate a QR-code for it, and place the QR-code in the ImageView where the default card was stored.

So far so good; however, there's an issue with the app remembering which image to show when navigating away from the result (another screen in the app, going to the homescreen of your device, etc) and going back to the screen. It shows the default card again.

Now, I understand I have to save the QR-code but I can't come up with a solution. The device has no SD-card so saving to external storage is not an option.

I've tried solving it via SharedPreferences, OutputStream, and the cache; but couldn't get it to work.

Which of those 3 ways (or maybe a different one, all help is welcome) would you choose and what would the code be for that?

解决方案

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;
    }
}

这篇关于我应该如何保存在Android上的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆