2015-09-08 16 views
0

我试图制作一种图像日记,可以在图像中创建帖子。首先我有问题,每个帖子都显示相同的图像,但试图用File.createTempFile修复它。但是现在什么都没有显示,我得到了很多空指针异常。图像不会显示在ListView中

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

我能看到的是,你需要尝试调试。另外你的日志在空对象引用上说“ContentItem.getImageNames()”首先检查它。 –

回答

0

里面你loadImageFromStorage(字符串路径,int i)以法 线

String name = p.getImageNames().get(i); 

是给你错误确保你在您的实例变量p上设置arraylist,然后尝试获取该列表。