2012-09-08 3 views
2

갤러리에서 이미지를 얻으려면 코딩을 시도하고 이미지를 표시 한 후 SdCard에 JPG로 저장하고 싶습니다. 어디서나 PSD에서 sdcard에 이미지를 저장하는 방법이 있으며 저장할 코드는 어디에 두어야합니까?sdcard a jpeg에 비트 맵 이미지를 저장하는 방법?

public void onClick(View arg0) { 
        Bitmap yourimage; 
        yourimage=toGrayscale(yourSelectedImage); 
        ImageButton effected=(ImageButton)findViewById(R.id.imageButton2); 
        int width = 150; 
        int height =150; 
        Bitmap resizedbitmap=Bitmap.createScaledBitmap(yourimage, width, height, true); 
        effected.setImageBitmap(resizedbitmap); 
} 

내가 SDCARD에 저장하려는 resizedbitmap로 내가 설정 한 이미지 후 평균

+0

가능한 중복, 또는 적어도 몇 가지 답변을 포함 http://stackoverflow.com/questions/649154/android-bitmap-save-to-location?rq=1 – mehmetminanc

+0

/때 만약 당신이 그들을 받아 들일 필요가 대답합니다. – WIllJBD

답변

7

이 기본적으로

Bitmap bmp = createBitmap(); 
OutputStream stream = new FileOutputStream("/sdcard/test.jpg"); 
bmp.compress(CompressFormat.JPEG, 100, stream); 
입니다 : 이 내가 button2를 클릭 한 후, 저장할 위치입니다
2

이미지가 bitmpa formate 인 경우, 예를 들어 앱의 스크린 샷을 찍은 다음 해당 비트 맵 이미지 객체를 sdcard의 이미지 파일로 "jpg"형식으로 저장하려는 경우 다음의 몇 줄을 사용하십시오. 코드, 희망 당신은 당신의 대답을 얻을 것입니다.

// ----------- 이미지 파일 이름 ..-------

OutputStream fOut = null; 
File file = new File("sdcard/pir_fahim_shah.jpg"); 
    try 
    { 
     fOut = new FileOutputStream(file); 
     b.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
     try 
     { 
      fOut.flush(); 
      fOut.close(); 
      } 
     catch (IOException e) 
     { e.printStackTrace(); } 
     } catch (FileNotFoundException e) 
      {  e.printStackTrace(); } 



try { 
    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); 
} 
catch (FileNotFoundException e) 
{  e.printStackTrace(); } 
4
**This Code Cover the Following Topics** 

1. Save a bitmap Image on sdcard a jpeg 
2. Create a folder on sdcard 
3. Create every file Separate name 
4. Every file save with date and time 
5. Resize the image in very small size 
6. Best thing image Quality fine not effected from Resizing 


The following method is used to create an image file using the bitmap 

공공 무효 createImageFromBitmap와 sdcard에있는 이미지 파일을 저장 (비트 맵 BMP) {

FileOutputStream fileOutputStream = null; 
    try { 

     // create a File object for the parent directory 
     File wallpaperDirectory = new File("/sdcard/Capture/"); 
     // have the object build the directory structure, if needed. 
     wallpaperDirectory.mkdirs(); 

     //Capture is folder name and file name with date and time 
     fileOutputStream = new FileOutputStream(String.format(
       "/sdcard/Capture/%d.jpg", 
       System.currentTimeMillis())); 

     // Here we Resize the Image ... 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, 
       byteArrayOutputStream); // bm is the bitmap object 
     byte[] bsResized = byteArrayOutputStream.toByteArray(); 


     fileOutputStream.write(bsResized); 
     fileOutputStream.close(); 


    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
    } 
} 


    and add this in manifest 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
관련 문제