将android.graphics.Bitmap转换为java.io.File

Kru*_*hal 5 android android-bitmap

我想使用这样的多部分上传将已编辑的位图图像上传到服务器,

multipartEntity.addPart("ProfilePic", new FileBody(file));
Run Code Online (Sandbox Code Playgroud)

但我无法将Bitmap(android.graphics.Bitmap)图像转换为File(java.io.File).

我试图将其转换为字节数组,但它也没有用.

有没有人知道android的内置功能或任何将Bitmap转换为File的解决方案?

请帮忙...

Sar*_*ran 26

这应该这样做:

private static void persistImage(Bitmap bitmap, String name) {
  File filesDir = getAppContext().getFilesDir();
  File imageFile = new File(filesDir, name + ".jpg");

  OutputStream os;
  try {
    os = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
    os.flush();
    os.close();
  } catch (Exception e) {
    Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
  }
}
Run Code Online (Sandbox Code Playgroud)

更改Bitmap.CompressFormat和扩展以适合您的目的.