Android的,保存和加载从diferent活动缓存的位图 [英] Android, Saving and loading a bitmap in cache from diferent activities

查看:105
本文介绍了Android的,保存和加载从diferent活动缓存的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的位图,需要在一个新的活动来展示,所以我cahe,并在打开的活动我尝试加载它,但我得到一个NullPointerException异常。在这里,我保存图片:

 文件cacheDir = getBaseContext()getCacheDir()。
文件F =新的文件(cacheDir,PIC);

尝试 {
    FileOutputStream中出=新的FileOutputStream(
            F);
    pic.com preSS(
            Bitmap.Com pressFormat.JPEG,
            100,出);
    了out.flush();
    out.close();

}赶上(FileNotFoundException异常E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
}

意向意图=新的意图(
        AndroidActivity.this,
        OpenPictureActivity.class);
startActivity(意向);
 

,然后在新的活动我尝试打开它:

 公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);

    文件cacheDir = getBaseContext()getCacheDir()。
    文件F =新的文件(cacheDir,PIC);
    的FileInputStream FIS = NULL;
    尝试 {
        FIS =新的FileInputStream(F);
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    点阵位图= BitmapFactory.de codeStream(FIS);

    ImageView的viewBitmap =(ImageView的)findViewById(R.id.icon2);
    viewBitmap.setImageBitmap(位);
    的setContentView(R.layout.open_pic_layout);
 

解决方案

只是检查你的code:

  ImageView的viewBitmap =(ImageView的)findViewById(R.id.icon2);
viewBitmap.setImageBitmap(位);
的setContentView(R.layout.open_pic_layout);
 

您已经设置内容的浏览之前写findViewById()。它错了。

 公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    的setContentView(R.layout.open_pic_layout);

  //这里做你的作业
  }
 

I have i bitmap that needs to show in a new activity, so i cahe it and in the opened activity i try to load it but i get a nullPointerException. Here i save the image :

File cacheDir = getBaseContext().getCacheDir();
File f = new File(cacheDir, "pic");

try {
    FileOutputStream out = new FileOutputStream(
            f);
    pic.compress(
            Bitmap.CompressFormat.JPEG,
            100, out);
    out.flush();
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Intent intent = new Intent(
        AndroidActivity.this,
        OpenPictureActivity.class);
startActivity(intent);

and then in the new activity i try to open it :

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    File cacheDir = getBaseContext().getCacheDir();
    File f = new File(cacheDir, "pic");     
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(fis);

    ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
    viewBitmap.setImageBitmap(bitmap);
    setContentView(R.layout.open_pic_layout);

解决方案

Just check your code:

ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
viewBitmap.setImageBitmap(bitmap);
setContentView(R.layout.open_pic_layout);

You have written findViewById() before setting Content View. Its wrong.

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.open_pic_layout);

  // do your operations here
  }

这篇关于Android的,保存和加载从diferent活动缓存的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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