2015-11-05 53 views
-2

下面是代码:我有错误在内存中的android显示图像文件保存,

public void Displayimg(View v) { 

    File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp"); 


    ipath[0] = String.valueOf(((TextView) v).getText()); 


    String sifile = ipath[0].substring(45,52); // extracting the filename from the view eg: abc.jpg 


    File imgfile = new File(path,sifile); // it fails on this line with unfortunately, main application has stopped. 

    // if the sifile conatians a name of the file that exist, it give error and comes out 
    // if I give file name in sifile that does not exisit, if give file does on exisit and comes our with error. 
    // Basically I am having problem to open an image file that exisit and dispaly. 


    // File("/storage/sdcard0/Pictures/MyCameraApp/Zimg20151105_1535133.Jpg"); 



    Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 

    ImageView myImage = (ImageView) findViewById(R.id.mc_imgview); 

    if(imgfile.exists()){ 

     Toast.makeText(getApplicationContext(),file.getAbsolutePath() + "File Exisit", Toast.LENGTH_SHORT).show(); 
     myImage.setImageBitmap(myBitmap); 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(),file.getAbsolutePath() + " File Does not Exisit", Toast.LENGTH_SHORT).show(); 
    } 


} 
+0

什么错误?请发布你的logcat输出 –

+0

你能解释那个.substring(45,52)的含义吗? – Nanoc

+0

什么是错误的家伙 – Tony

回答

1

显示图片:

尝试搜索VolleyUniversal-Image-LoaderGlide

保存图像:

public static String getSdPath(){ 
     //todo test path 
     return Environment.getExternalStorageDirectory().getAbsolutePath(); 
//  return ""; 
    } 

    public static String getImageDir (String type,Activity activity){ 
     if(type.equalsIgnoreCase("pure")){ 
      return getSdPath()+ activity.getDir("pure", Context.MODE_PRIVATE).getAbsolutePath(); 
     }else{ 
      return getSdPath()+activity.getDir("deal", Context.MODE_PRIVATE).getAbsolutePath(); 
     } 
    } 

    private static final String APPLICATION_NAME = "test"; 
    private static final Uri IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    private static final String PATH = getImageDir("deal", mActivity); 

    public static Uri savePngImage(ContentResolver cr, Bitmap bitmap) { 
     long dateTaken = System.currentTimeMillis(); 
     String name = String.valueOf(dateTaken) + ".png"; 
     return savePngImage(cr, name, dateTaken, PATH, name, bitmap); 
    } 

    public static Uri savePngImage(ContentResolver cr, String name, long dateTaken, String directory, 
            String filename, Bitmap source) { 

     OutputStream outputStream = null; 
     String filePath = directory + File.separator + filename; 
     try { 
      File dir = new File(directory); 
      if (!dir.exists()) { 
       dir.mkdirs(); 
      } 
      File file = new File(directory, filename); 
      if (file.createNewFile()) { 
       outputStream = new FileOutputStream(file); 
       if (source != null) { 
        source.compress(Bitmap.CompressFormat.PNG, 100, outputStream); 
       } else { 
       } 
      } 
//   FileUtils.updateFile(file); 
     } catch (FileNotFoundException ex) { 
      return null; 
     } catch (IOException ex) { 
      return null; 
     } catch (NullPointerException ex) { 
      return null; 
     }finally { 
      if (outputStream != null) { 
       try { 
        outputStream.close(); 
       } catch (Throwable t) { 
       } 
      } 
     } 

     ContentValues values = new ContentValues(7); 
     values.put(MediaStore.Images.Media.TITLE, name); 
     values.put(MediaStore.Images.Media.DISPLAY_NAME, filename); 
     values.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken); 
     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
     values.put(MediaStore.Images.Media.DATA, filePath); 
//  FileUtils.updateFile(filePath); 
     return cr.insert(IMAGE_URI, values); 
    } 
+0

通用图像加载器是伟大的! –

相关问题