How to save image in internal storage
Direct saveing image
private void saveImage(Bitmap imageToSave) {
File direct = new File(Environment.getExternalStorageDirectory() + "/Samset_ps");
Log.e("Logger", " Step 1 ");
if (!direct.exists()) {
File scanDoc = new File("/sdcard/Scanner_ps/");
scanDoc.mkdirs();
Log.e("Logger", " Step 2 ");
}
Log.e("Logger", " Step 3 ");
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File file = new File(new File("/sdcard/Scanner_ps/"), fileName);
Log.e("Logger", " Step 4 file name "+file.toString());
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
Log.e("Logger", " Step 5 ");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
File direct = new File(Environment.getExternalStorageDirectory() + "/Samset_ps");
Log.e("Logger", " Step 1 ");
if (!direct.exists()) {
File scanDoc = new File("/sdcard/Scanner_ps/");
scanDoc.mkdirs();
Log.e("Logger", " Step 2 ");
}
Log.e("Logger", " Step 3 ");
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File file = new File(new File("/sdcard/Scanner_ps/"), fileName);
Log.e("Logger", " Step 4 file name "+file.toString());
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
Log.e("Logger", " Step 5 ");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
With Asyntask
This is the best way save image
// you can change input type like byte,bitmap
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
Log.e("Logger", " Step 1 ");
// Write to SD Card
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/Samset_ps");
if (!dir.exists())
{
dir.mkdirs();;
}
Log.e("Logger", " Step 2 ");
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
Log.e("Logger", " Step 3 ");
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
Log.e("Logger", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
Log.e("Logger", " Step 1 ");
// Write to SD Card
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/Samset_ps");
if (!dir.exists())
{
dir.mkdirs();;
}
Log.e("Logger", " Step 2 ");
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
Log.e("Logger", " Step 3 ");
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
Log.e("Logger", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
No comments:
Post a Comment