将位图转换为文件

Mxy*_*xyk 115 android file bitmap

我知道使用BitmapFactory可以将文件转换为位图,但有没有办法将位图图像转换为文件?

ams*_*ddh 218

希望它能帮助你:

//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();

//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记刷新并关闭输出流:) (33认同)
  • 人们想知道质量指标是什么.它是0低至100的等级,高类似于photoshop导出等.如上所述,它被PNG忽略,但你可能希望使用`CompressFormat.JPEG`.根据谷歌doco:*提示压缩机,0-100.0表示压缩小尺寸,100表示​​压缩以获得最高质量.某些格式,如无损的PNG,将忽略质量设置* (9认同)
  • 代码工作正常,但压缩方法需要很多时间.有什么工作吗? (3认同)
  • 缓存目录中的文件是否会被自动删除? (3认同)

P.M*_*lch 77

试试这个:

bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
Run Code Online (Sandbox Code Playgroud)

看到这个

  • 但我不想要一个`FileOutputStream`,只需一个文件.有没有解决的办法? (9认同)
  • 还有什么是"质量"? (8认同)

fra*_*kee 34

File file = new File("path");
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Run Code Online (Sandbox Code Playgroud)


Moh*_*aby 9

转换BitmapFile需要在后台完成(不在主线程中),如果它bitmap很大,它会挂起UI

File file;

public class fileFromBitmap extends AsyncTask<Void, Integer, String> {

    Context context;
    Bitmap bitmap;
    String path_external = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";

    public fileFromBitmap(Bitmap bitmap, Context context) {
        this.bitmap = bitmap;
        this.context= context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before executing doInBackground
        // update your UI
        // exp; make progressbar visible
    }

    @Override
    protected String doInBackground(Void... params) {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
        try {
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        // back to main thread after finishing doInBackground
        // update your UI or take action after
        // exp; make progressbar gone

         sendFile(file);

    }
}
Run Code Online (Sandbox Code Playgroud)

打电话给它

new fileFromBitmap(my_bitmap, getApplicationContext()).execute();
Run Code Online (Sandbox Code Playgroud)

你必须使用filein onPostExecute.

要更改file要存储在缓存替换行中的目录:

 file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
Run Code Online (Sandbox Code Playgroud)

用:

file  = new File(context.getCacheDir(), "temporary_file.jpg");
Run Code Online (Sandbox Code Playgroud)


Asa*_*hry 9

大多数答案要么太长要么太短,达不到目的。对于那些正在寻找 Java 或 Kotlin 代码来将位图转换为文件对象的人。这是我写的关于该主题的详细文章。在 Android 中将位图转换为文件

public static File bitmapToFile(Context context,Bitmap bitmap, String fileNameToSave) { // File name like "image.png"
        //create a file to write bitmap data
        File file = null;
        try {
            file = new File(Environment.getExternalStorageDirectory() + File.separator + fileNameToSave);
            file.createNewFile();

//Convert bitmap to byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos); // YOU can also save it in JPEG
            byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            return file;
        }catch (Exception e){
            e.printStackTrace();
            return file; // it will return null
        }
    }
Run Code Online (Sandbox Code Playgroud)