How to create Image File inside Internal Cache Directory in Android - android

I want to have image.png inside cache/images/ in Internal Storage of Android.
I am not able to have it with following code:
File directory = new File(getContext().getCacheDir(), "images");
directory.mkdirs();
File mypath=new File(directory,"image.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
With the above code I am not even able to create a directory named images. Pls help, I am beginner.

Try this:
File sd = getCacheDir();
File folder = new File(sd, "/myfolder/");
if (!folder.exists()) {
if (!folder.mkdir()) {
Log.e("ERROR", "Cannot create a directory!");
} else {
folder.mkdirs();
}
}
File fileName = new File(folder,"mypic.jpg");
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

I have implemented it like this...
//create file
File file = new File(context.getExternalCacheDir(), System.currentTimeMillis() + ".png");
//draw image if created successfully
if (file.createNewFile()) {
//initialize image BitMap
Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
//initialize canvas and paint object
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
//design part goes here using Paint and Canvas
...
//get file output stream and draw design on image file
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}

Related

write image in external directory android

How to write image in phone directory. Directory is created but i could not write image in this folder. Here is my code .please check if i am doing something wrong thanks in advance.
Bitmap bitmap;
String directory = new_path; // I am getting path here of image like sdcard/0/emulated/image.jpg
String folder_name = "abc"
bitmap = BitmapFactory.decodeFile(directory);
iv_6.setImageBitmap(bitmap); // image displayed here but not saving in directory.
try {
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator +folder_name);
f.mkdirs();
FileOutputStream fo = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fo);
fo.close();
}catch (Exception e)
{
e.printStackTrace();
}
public void savePng(Bitmap bitmap, String filePath) {
try {
File temp = new File(filePath);
FileOutputStream os = new FileOutputStream(temp + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

write an image to external storage in android

I use following code to write an image to external storage in android :
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
fileName = "image_2.jpeg";
File file = new File(dir, fileName);
try {
FileOutputStream outStream = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeFile("android.resource://com.mypackage.com/drawable/image_1");
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This code is for reading image_1.jpg from drawable folder, then writing it to download folder in external storage, with image_2.jpeg name. (create download folder in external storage and a file with image_2.jpeg name inside that folder).
This code will produce an ((force close)). download folder is created and also the image_2.jpeg is created, but image image_2.jpeg is corrupted.
These images in drawable folder can be accessed by BitmapFactory, you can save the bitmap to PNG or JPG.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission.
For other type of images, I think put them into assets folder is a better way.
There is a sample here.
I did same thing with this code.Try this code:
String[] sampleImagesName = { "image2" };
int[] sampleImages = { R.drawable.image1};
File file;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/download");
if (!file.exists()) {
file.mkdirs();
SaveSampleToSD();
}
}
private void SaveSampleToSD() {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/download";
for (int i = 0; i < sampleImages.length; i++) {
try {
File f = new File(path + "/", sampleImagesName[i] + ".jpg");
Bitmap bm = BitmapFactory.decodeResource(getResources(),
sampleImages[i]);
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
Log.e("ImageSaved---------", "saved");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Get screenshot of surfaceView in Android

In my application I have view with some buttons and SurfaceView. I must take screenshot of this view, but in place of SurfaceView is only black field. I try solutions which I found on stackoverflow, but they don't work for me.
This is my code which I usualy take screenshots:
File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/folder");
if (!folder.exists()) {
folder.mkdir();
}
String path = folder.getAbsolutePath() + "snapshot.png";
File file = new File(path);
file.createNewFile();
View view = this.findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
bitmap.recycle();
view.setDrawingCacheEnabled(false);
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
Maybe someone can help.

How to store bitmap image in file in sdcard?

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
cacheFile = new File(android.os.Environment.getExternalStorageDirectory(), "Android/data/"+this.getPackageName()+"/cache/");
if(!cacheFile.exists())
{
cacheFile.mkdir();
}
try {
fileOutputStream = new FileOutputStream(cacheFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
Log.i("Error", e.getMessage());
}
}
but I am getting error that mnt/sdcard/Android/data/com.android.linegame/cache file not exist
Which versoin of Android you are using?? Because Android 2.2 contains folder "Android" in mnt/sdcard. But Android 4.0 version doesn't contain folder "Android".
Add following code:
cacheFile = new File(Environment.getExternalStorageDirectory(), "/Android/data/"+this.getPackageName()+"/cache/");
if(!cacheFile.exists()) {
cacheFile.mkdirs();
}

How I can guarantee that Android will create my files

I wrote this code to save my canvas as bitmap but it didn't work. Can anyone help?
public void saveImage(){
try {
Bitmap bitmap = object.getDrawingCache();
path = Environment.getDataDirectory().getAbsolutePath();
file = new File( path.toString() +"/image.png");
FileOutputStream fos ;
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
isFileCreated = file.exists();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
Without knowing what you mean by "it didn't work", try creating the File differently. This is better than manually trying to build the path:
file = new File(Environment.getDataDirectory(), "image.png");

Categories

Resources