Store Bitmap image to SD Card in Android

前端 未结 3 1113
鱼传尺愫
鱼传尺愫 2021-01-05 19:01

I am facing some strange problem with my android code, I have an image in Bitmap variable and want to save that file to SD card. I code as follow,

Bitmap IMA         


        
3条回答
  •  盖世英雄少女心
    2021-01-05 19:35

    **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
    
      
    

提交回复
热议问题