如何在sdcard上保存位图图像jpeg?

时间:2012-09-08 23:10:54

标签: android image bitmap save jpeg

我正在尝试对应用程序进行编码以从图库中获取图像,然后对其进行一些效果,并在显示图像后,我想将其保存为SdCard中的JPG。 任何人都可以告诉我如何在SD卡上保存图像,我应该在哪里放保存代码? 这是我想要保存的地方,点击button2:

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

我的意思是在将图片设置为 resizedbitmap 之后我想将其保存在sdcard中

3 个答案:

答案 0 :(得分:7)

这基本上就是

Bitmap bmp = createBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
bmp.compress(CompressFormat.JPEG, 100, stream);

答案 1 :(得分: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

public void createImageFromBitmap(Bitmap 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" /> 

答案 2 :(得分:2)

如果您有一个bitmpa formate中的图像,例如,如果您拍摄应用程序的屏幕截图,现在您想将该位图图像对象作为图像文件以“jpg”格式存储在sdcard中,那么请使用这几行代码,希望你能得到答案。

// -----------将图像文件存储在sdcard中,图像文件名为..-------

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