将FrameLayout转换为jpeg文件

时间:2017-07-22 21:38:15

标签: android image bitmap android-framelayout

我一直在尝试将包含图片和一些文字的FrameLayout转换为位图,然后将其导出为jpg,虽然这很成功,但生成的jpg质量非常低。

我就是这样做的:

public Bitmap viewToBitmap(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

private void saveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Test");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();  

    } catch (Exception e) {
        e.printStackTrace();
    }
}

    Bitmap image = viewToBitmap(frameLayout);
    saveImage(image);

有没有办法以更高的质量导出相同的FrameLayout?谢谢!

0 个答案:

没有答案