我试图制作一种图像日记,用图像创建帖子。首先我遇到的问题是每个帖子都显示相同的图像,但尝试用File.createTempFile修复它。但是现在什么都没有显示,我得到了很多空指针异常。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/**
*Gets the images taken with the camera, depended on how many pictures were taken, they are saved in images.
**/
if (requestCode == CAMERA_REQUEST && resultCode == this.getActivity().RESULT_OK && counter <= 2) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
switch (counter) {
case 0:
im1.setVisibility(View.VISIBLE);
im1.setImageBitmap(bmp);
Log.d(this.getActivity().getPackageName(), bmp != null ? "bmp is not null!" : "bmp is null!");
images.add(saveToInternalStorage(bmp));
counter++;
break;
case 1:
im2.setVisibility(View.VISIBLE);
im2.setImageBitmap(bmp);
//pathList.add(1,saveToInternalStorage(bmp));
images.add(saveToInternalStorage(bmp));
counter++;
break;
case 2:
im3.setVisibility(View.VISIBLE);
im3.setImageBitmap(bmp);
images.add(saveToInternalStorage(bmp));
counter++;
break;
}
...
private String saveToInternalStorage(Bitmap bitmapImage) {
ContextWrapper cw = new ContextWrapper(this.getActivity().getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath = null;
try {
mypath = File.createTempFile("image", ".jpg", directory);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
...在我的适配器中
private Bitmap loadImageFromStorage(String path) {
Bitmap b = null;
File f= null;
try {
f = new File(path);
b = BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return b;
}
更新了上面的代码,但没有我得到这个例外。我想也许错误是在File.createTempFile保存图像时,加载函数再次获取图像,这可能是我错过的东西吗?
修复了异常,但我仍然没有获得图片
答案 0 :(得分:0)
在 loadImageFromStorage(String path,int i)方法中 线
String name = p.getImageNames().get(i);
给出错误,确保您在实例变量p上设置了arraylist,然后尝试获取该列表。