2014-06-07 5 views
-3

나는 png 파일을 다시 bmp로 변환하고 sdcard에 저장하는 코드를 작성하고 있습니다. 이것은 현재 코드입니다.SdCard 안드로이드에 비트 맵 저장

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을 Sdcard에 저장하는 데 문제가 있습니다. 내가 찾은 모든 예제는 bMap.compress()를 사용합니다. 이 방법을 사용하면 bmp로 저장할 수 없습니다. 누군가가 Sdcard에 비트 맵을 저장하는 방법에 대한 예제를 줄 수 있습니까?

편집 : 이제 파일을 .bmp로 sdcard에 저장할 수 있습니다. 그러나 원래 크기로되지 않습니다. PNG를 BMP로 변환하는 데 어떤 sugguestions?

+0

는'비트 맵 .compress (Bitmap.CompressFormat.JPEG, 80, 아웃)', 저장된 이미지의 품질이 항상 2를 변경하여 가장 적합한을 위해 스스로를 시도해야하므로 변경 매개 변수 – CodeWarrior

+0

압축 방법을 사용할 때 화질이 변하는 것을 알고 있습니다. 그러나 나는 JPEG를 다시 BMP로 변환 할 방법을 찾고있다. 원래 BMP는 편집 된 코드를 사용하는 80Mb와 같으며 14KB가됩니다. 분명히 압축 풀기가 작동하지 않습니다. BMP로 다시 변환 할 수 있습니까? (JPEG에서) 안드로이드에서 할 수 있습니까? – 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

관련 문제