use*_*941 9 android caching bitmap android-activity
我有一个需要在新活动中显示的位图,所以我想它并在打开的活动中我尝试加载它但我得到一个nullPointerException.在这里我保存图像:
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);
Run Code Online (Sandbox Code Playgroud)
然后在新活动中我尝试打开它:
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);
Run Code Online (Sandbox Code Playgroud)
只需检查您的代码:
ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
viewBitmap.setImageBitmap(bitmap);
setContentView(R.layout.open_pic_layout);
Run Code Online (Sandbox Code Playgroud)
您在设置内容视图之前编写了findViewById().这是不对的.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.open_pic_layout);
// do your operations here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10355 次 |
| 最近记录: |