2014-06-07 168 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