0

I have created a video thumbnail from a video file. What I need to do is to create a java.io.File from the Bitmap in order to upload the thumbnail to a server. How could achieve this?

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(videoFile.getPath(), MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);

File thumbFile = ?
SNos
  • 3,430
  • 5
  • 42
  • 92
  • Code to do do has been posted here so often. Just google a bit. Or read a twenty pages tagged android. In principle you compress the bitmap to a file output stream. – greenapps Dec 25 '16 at 11:53
  • Thanks .. Have been looking for it but seems I cannot find it. – SNos Dec 25 '16 at 11:54
  • This will help you http://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android – MdFazlaRabbiOpu Dec 25 '16 at 15:39

1 Answers1

0

can use the following code:

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

//f.createNewFile(); (No need to call as FileOutputStream will automatically create the file)

//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();

you can also try this method: it does not convert bitmap to byte Array..

private static void bitmap_to_file(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);
  }
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62