简体   繁体   中英

save image file with bitmap factory

I'm encountering an issue with bitmap factory. I've got a method to reduce and rotate an image to show a preview in an image view, but I would like to save this with the new size. I'm just turning around with inputfilestream and outputfilestream but don't get to save it. Is anybody know a clear method to put my bitmap in an outpufilestream? Thanks a lot here's my code

@Override
protected void onResume() {
    super.onResume();
    File[] fileArray;
    final File root;
    File chemin = Environment.getExternalStorageDirectory();
    String filepath = chemin + "/SmartCollecte/PARC/OUT/" + fichano + "_" + conteneur_s+"_"+cpt+".jpg";


    try {
        decodeFile(filepath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }}


public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap b1 = BitmapFactory.decodeFile(filePath, o2);
    Bitmap b = ExifUtils.rotateBitmap(filePath, b1);
    FileOutputStream fos = new FileOutputStream(filePath);
    b.compress(Bitmap.CompressFormat.PNG,100,fos);
    fos.close();
    showImg.setImageBitmap(b);


}

Have you tried doing it like this? Assuming bitmap is bitmap you want to save.

Also, take a look at some existing system directories.

final FileOutputStream fos = new FileOutputStream(new File(filepath + "_scaled.jpg"));
try {
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
} catch (IOException e) {
    // handle exception
} finally {
    fos.close
}

Source

Where first parameter of Bitmap.compress() is your desired output format (see CompressFormat ) and the second parameter is compression quality.

ok I found out what was missing. had to create a new byte array to convert my bitmap to file :

String filepathcomp = Environment.getExternalStorageDirectory()+"/SmartCollecte/PARC/OUT/"+ fichano + "_" + conteneur_s+"_"+cpt+".jpg";
    File f = new File(filepathcomp);
    Bitmap newbitmap = b;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    newbitmap.compress(Bitmap.CompressFormat.JPEG,80,bos);
    byte[] bitmapdata = bos.toByteArray();
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM