2015年2月8日 星期日

Android internal storage file

//save

public static boolean storeInsideImage(Context context, Bitmap image, String filepath, String filename) {


 File infilepath = new File(context.getFilesDir().getAbsoluteFile(), filepath);
 if (!infilepath.exists()) {
  infilepath.mkdirs();
 }

 try {

  File imgfile = new File(infilepath, filename);
  if (!imgfile.exists()){
   imgfile.createNewFile();
  }
  FileOutputStream fileOutputStream = new FileOutputStream(imgfile);
  BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
  // choose another format if PNG doesn't suit you
  image.compress(CompressFormat.JPEG, 100, fileOutputStream);
//   fileOutputStream.flush();
//   fileOutputStream.close();
  bos.flush();
  bos.close();
  return true;
 } catch (FileNotFoundException e) {
  Log.w("TAG", "Error saving image file: " + e.getMessage());
  return false;
 } catch (IOException e) {
  Log.w("TAG", "Error saving image file: " + e.getMessage());
  return false;
 }
 }


//load & delete

private void loadImageFromStorage(String path)
{

   try {
    //先刪除上一張
    //File dir = getFilesDir();
    File file = new File("/data/data/com.udn.nm.food/files/GoodFood/data/Files", "login.jpg");
    boolean deleted = file.delete();
    Log.d("ddddddd", "ddddddd deleted"+deleted);
   
       File f=new File(path, "login.jpg");

       Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
       if(b !=null){
        imageView1.setImageBitmap(b);
       }
       else{
        imageView1.setBackgroundResource(R.drawable.login_bg);
       }
     
         //  ImageView img=(ImageView)findViewById(R.id.imgPicker);
      // img.setImageBitmap(b);
   }
   catch (FileNotFoundException e)
   {
    imageView1.setBackgroundResource(R.drawable.login_bg);
       e.printStackTrace();
   }

}

}