create Image File - Android android.graphics

Android examples for android.graphics:Image Load Save

Description

create Image File

Demo Code

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.widget.Toast;

public class Main {

  public static File createImageFile(Context context, byte[] buffer, File file) {
    File cacheDir = context.getCacheDir();
    try {/*from  w  w w.j  a  va2s  . c o m*/
      if (!cacheDir.isDirectory() || !cacheDir.exists())
        cacheDir.mkdir();
    } catch (Exception e) {
      Toast.makeText(context, "Create Folder Error about Cache Directory", Toast.LENGTH_SHORT).show();
    }

    String path = file.getName();
    int index = path.lastIndexOf('\\');
    String name = path.substring(index + 1);

    File imagefile = new File(cacheDir, name);
    FileOutputStream fileWriter = null;

    try {
      fileWriter = new FileOutputStream(imagefile);
      fileWriter.write(buffer);
      fileWriter.flush();
    } catch (IOException e1) {
      e1.printStackTrace();
    } finally {
      try {
        fileWriter.close();
      } catch (IOException e) {
        System.err.println(e.getMessage());
      }
    }
    return imagefile;
  }

}

Related Tutorials