簡體   English   中英

Android:在內部存儲目錄中將捕獲的圖像格式從JPEG保存為PNG

[英]Android:save captured image format from JPEG to PNG in internal storage directory

在Eclipse中,我從模擬器捕獲了圖像,並將其保存為JPEG。但是我想將圖像保存為PNG。 如何將圖像格式從JPEG轉換為PNG。

在這里,我使用此代碼將圖像保存在目錄中。

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
   String photoFile = "Picture_" + date + ".PNG";
// String photoFile = "Picture_" + date + ".JPEG";
    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    File pictureFile = new File(filename);

    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);

      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();
    } 

提前致謝。

這里是使用Bitmap類創建圖像文件的代碼

編輯

您需要先將現有的圖像文件打開到該bmp對象中,然后在文件對象中輸入另一個名稱進行保存。

Bitmap bmp = BitmapFactory.decodeFile(pathName);// here you need to pass your existing file path


File image = new File(your_sdcard_file_path);
FileOutputStream outStream;
try {
    image.createFile(); // need to create file as empty first if it was exist already then change the name
    outStream = new FileOutputStream(image);
// here you need to pass the format as I have passed as PNG so you can create the file with specific format
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    /* 100 to keep full quality of the image */

    outStream.flush();
    outStream.close();
    success = true;
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM