猿问

使用位图工厂保存图像文件

我遇到了位图工厂的问题。我有一种方法可以缩小和旋转图像以在图像视图中显示预览,但我想用新尺寸保存它。我只是用 inputfilestream 和 outputfilestream 转过身来,但没有保存它。有人知道将我的位图放入输出文件流的明确方法吗?非常感谢这是我的代码


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



}


至尊宝的传说
浏览 173回答 2
2回答

料青山看我应如是

你试过这样做吗?假设bitmap是您要保存的位图。另外,看看一些现有的系统目录。final FileOutputStream fos = new FileOutputStream(new File(filepath + "_scaled.jpg"));try {&nbsp; &nbsp; bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);} catch (IOException e) {&nbsp; &nbsp; // handle exception} finally {&nbsp; &nbsp; fos.close}其中 Bitmap.compress() 的第一个参数是您想要的输出格式(请参阅 参考资料CompressFormat),第二个参数是压缩质量。

胡说叔叔

好的,我发现缺少了什么。必须创建一个新的字节数组来将我的位图转换为文件:String filepathcomp = Environment.getExternalStorageDirectory()+"/SmartCollecte/PARC/OUT/"+ fichano + "_" + conteneur_s+"_"+cpt+".jpg";&nbsp; &nbsp; File f = new File(filepathcomp);&nbsp; &nbsp; Bitmap newbitmap = b;&nbsp; &nbsp; ByteArrayOutputStream bos = new ByteArrayOutputStream();&nbsp; &nbsp; newbitmap.compress(Bitmap.CompressFormat.JPEG,80,bos);&nbsp; &nbsp; byte[] bitmapdata = bos.toByteArray();&nbsp; &nbsp; FileOutputStream fos = new FileOutputStream(f);&nbsp; &nbsp; fos.write(bitmapdata);&nbsp; &nbsp; fos.flush();&nbsp; &nbsp; fos.close();
随时随地看视频慕课网APP

相关分类

Java
我要回答