2013-04-03 54 views
0

我是Android新手。我在Eclipse中使用相機應用程序。捕獲的圖像存儲到應用程序文件夾(內部存儲)。圖像以JPEG格式存儲,但我希望它保存爲PNG格式。但是,我不想將圖像保存在外部存儲目錄將捕獲的圖像保存爲PNG的JPEG

這裏是我的代碼:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
     String date = dateFormat.format(new Date()); 
     String photoFile = "Picture_" + date + ".JPEG"; 

     String filename = pictureFileDir.getPath() + File.separator + photoFile; 
     File pictureFile = new File(filename);  


     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      bmp.compress(Bitmap.CompressFormat.PNG,100,fos); 

      fos.flush(); 
      // fos.write(data); 
      fos.close(); 
      Toast.makeText(context, "New Image saved:" + photoFile,Toast.LENGTH_LONG).show(); 
     } catch (Exception error) { 
     //Log.d(IntersaActivity.DEBUG_TAG, "File" + filename + "not saved: "+ error.getMessage()); 
     Toast.makeText(context, "Image could not be saved.", 
      Toast.LENGTH_LONG).show(); 
    } 
    } 

    private File getDir() { 
     String filepath = "MyFileStorage"; 
     ContextWrapper contextWrapper = new ContextWrapper(context); 
     File sdDir = contextWrapper.getDir(filepath, Context.MODE_PRIVATE); 
    //File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    return new File(sdDir, "CameraAPIDemo"); 
    } 
} 
+1

代替使用'.JPEG'擴展名,只需將其改爲'.PNG',您的圖像將被保存爲PNG格式。 'String photoFile =「Picture_」+ date +「.PNG」;'改變這一行。 – GrIsHu

+0

@Luiggi門多薩:謝謝你的迴應。實際上我改變了這一行像「.PNG」。它將作爲「.PNG」存儲在內部存儲器中,當我用Paint打開該圖片時,只有當我點擊時才顯示JPEG類型格式SaveAs按鈕(通常一些「.PNG」圖像用繪畫方式打開,當我點擊SaveAs按鈕時,它顯示PNG格式)。這是什麼解決方案。提前致謝。 – manoj

+3

對不起,我還沒有發佈任何形式的答案或建議= \ –

回答

0

使用

private void savePicture(String filename, Bitmap b, Context ctx){ 
    try { 
     ObjectOutputStream oos; 
     FileOutputStream out;// = new FileOutputStream(filename); 
     out = ctx.openFileOutput(filename, Context.MODE_PRIVATE); 
     oos = new ObjectOutputStream(out); 
     b.compress(Bitmap.CompressFormat.PNG, 100, oos); 

     oos.close(); 
     oos.notifyAll(); 
     out.notifyAll(); 
     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

,並設置文件名的擴展名是PNG

希望它能幫助。

編輯:您調用此方法的總代碼。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
String date = dateFormat.format(new Date()); 
String photoFile = "Picture_" + date + ".PNG"; 

String filename = pictureFileDir.getPath() + File.separator + photoFile; 

try { 
    savePicture(filename, bmp, context); 
    Toast.makeText(context, "New Image saved:" + photoFile,Toast.LENGTH_LONG).show(); 
} catch (Exception error) { 
    Toast.makeText(context, "Image could not be saved.", Toast.LENGTH_LONG).show(); 
} 
+0

@ Grishu,@ Luiggi Mendoza,@ Sri Harsha Chilapati:謝謝你的迴應。如果我從瀏覽器下載PNG圖像並用油漆打開並單擊SaveAs按鈕意味着它會在SaveAs Type中顯示PNG格式。但是,如果我下載從內部存儲器中捕獲圖像(.PNG)並用油漆打開,然後單擊SaveAs表示它在SaveAs Type中顯示JPEG格式。這是爲什麼?..在此先感謝。 – manoj

+0

@Manojbabu你能告訴你如何使用這種方法? –

+0

:我已經更新了上面的代碼。它顯示錯誤(局部變量oos可能沒有被初始化)。但是,我初始化null值到oos並捕獲圖像,然後它不能將圖像存儲在APPLICATION FOLDER(內部存儲)。提前感謝。 – manoj

相關問題