2

我正在尝试编写一个应用程序以从图库中获取图像然后对其进行一些效果,并且在显示图像后,我想将其保存为 SdCard 中的 JPG。可以请告诉我如何将图像保存在 sdcard 上,我应该将保存代码放在哪里?这是我要保存的地方,点击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 中

4

3 回答 3

7

基本上就是这样

Bitmap bmp = createBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
bmp.compress(CompressFormat.JPEG, 100, stream);
于 2012-09-08T23:25:23.517 回答
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(位图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" /> 
于 2014-05-16T05:47:11.267 回答
2

如果您有一个 bitmpa 格式的图像,例如,如果您拍摄应用程序的屏幕截图,并且现在您想将该位图图像对象存储为“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();   }
于 2013-04-10T20:47:25.710 回答