2014-06-07 225 views
-3

我正在編寫一個代碼將png文件轉換回bmp並將其保存在SD卡上。這是我現在的代碼。將位圖保存到SD卡Android

FileInputStream in; 
    BufferedInputStream buf; 
    try { 
     in = new FileInputStream("File_Path_to_Read.png"); 
     buf = new BufferedInputStream(in); 
     byte[] bMapArray= new byte[buf.available()]; 
     buf.read(bMapArray); 
     Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); 

     //Code segment to save on file 
     int numBytesByRow = bMap.getRowBytes() * bMap.getHeight(); 
     ByteBuffer byteBuffer = ByteBuffer.allocate(numBytesByRow); 
     bMap.copyPixelsToBuffer(byteBuffer); 
     byte[] bytes = byteBuffer.array(); 

     FileOutputStream fileOuputStream = new FileOutputStream("File_Path_To_Save.bmp"); 
     fileOuputStream.write(bytes); 
     fileOuputStream.close(); 


     if (in != null) { 
     in.close(); 
     } 
     if (buf != null) { 
     buf.close(); 
     } 


    } catch (Exception e) { 

    } 

我在保存bMap到SD卡時遇到問題。我找到的所有例子都使用bMap.compress()。使用這種方法我不能保存爲BMP。有人可以舉例說明如何在Sdcard上保存位圖嗎?

編輯: 我現在可以將文件保存爲.bmp到SD卡。但它不會達到原始大小。任何關於將PNG轉換爲BMP的建議?

+0

'位.compress(Bitmap.CompressFormat.JPEG,80出)',只要您使用此方法,保存的圖像質量的變化總是讓你不得不改變2親自試一試的最合適該方法的參數。 – CodeWarrior

+0

我知道使用壓縮方法時圖像質量發生了變化。但是,我正在尋找一種方法將JPEG轉換回BMP。原始的BMP就像使用我編輯的代碼80 Mb,我得到14 KB。很顯然,解壓縮不工作...可以轉換回BMP(從JPEG)在Android上完成? – user340

回答

0
File root = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "My_Folder" + File.separator); 
    root.mkdirs(); 
    File myFile = new File(root, "ABC.jpg"); 
    Bitmap bitmap = decodeFile(myFile, 800, 600); 
    OutputStream out = null; 
    File file = new File(mediaStorageDir.getAbsoluteFile() + "/DEF.jpg"); 
    try { 
     out = new FileOutputStream(file); 
     bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out); 
     out.flush(); 
     out.close(); 
     bitmap.recycle(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

public static Bitmap decodeFile(File f, int WIDTH, int HIGHT) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_WIDTH = WIDTH; 
     final int REQUIRED_HIGHT = HIGHT; 
     // Find the correct scale value. It should be the power of 2. 
     int scale = 2; 
     while (o.outWidth/scale/2 >= REQUIRED_WIDTH 
       && o.outHeight/scale/2 >= REQUIRED_HIGHT) 
       scale *= 2; 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) { 
    } 
return null; 
} 
+1

只需將信息添加到您的回覆中:Environment.getExternalStorageDirectory()不會讓您訪問外部SD卡。 – greywolf82

+0

@ greywolf82我假設 ''將在清單中提供:) – CodeWarrior

+0

@AndroidWarrior這是保存爲.JPEG格式。我想將ABC.JPEG轉換爲DEF.bmp。這可能嗎?用這個代碼 - 位圖.compress(Bitmap.CompressFormat.JPEG,80,out); – user340