How I can guarantee that Android will create my files - android

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");

Related

How to create Image File inside Internal Cache Directory in 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();
}

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

Can't save compressed file to temporary storage Android

I have some code to create a temporary file to save a compressed image file to it before sending it out to the server. The image gets to the server and is saved. The problem is that the file is 0 kB and I'm sure the compression isn't that good. It looks like the file isn't getting saved to the fileOutputStream, but after reading the documentation and examples, I'm not seeing why not. Also, the bitmap has 8294400 bytes and the saved file has a length of 0. That's why I think something happens while being sent out on fileOutputStream.
BitmapDrawable drawable = (BitmapDrawable) profileImage.getDrawable();
Bitmap bmap = drawable.getBitmap();
String filename = "tempImage";
File file = null;
try {;
file = File.createTempFile(filename, null, getApplicationContext().getCacheDir());
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fout = null;
try {
fout = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fout = openFileOutput(filename, MODE_PRIVATE);
bmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException 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();
}
}
}

After saving my bitmap to a file the size is 0 KB

I have a android app where I have a bitmap and I want to save it to the application data folder. The file is there after execution, but its 0kb and no picture is inside.
Where is the bug?
Here is my code:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(projDir + File.separator + newPath);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
add fo.flush()
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush()
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Try to add fo.flush()
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush()
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Edit
try this:
File f = new File(projDir + File.separator + newPath);
FileOutputStream out = new FileOutputStream(f);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, out);
out.flush();
out.close();
Try it with FileOutputStream:
try {
FileOutputStream fos= new FileOutputStream(projDir + File.separator + newPath);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, fos);
} catch (Exception e) {
}
There is no need to call createNewFile() , it will be automatically created if it does not exist. I guess since you never delete it is already there and it is not created because of that.
Also as a good habit you should put clean up related code inside the finally block. This way if some error happens somewhere the file will be eventually closed.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
boolean success = myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
if(!success) {
Log.w("myApp", "cannot compress image");
}
String patg = projDir + File.separator + newPath
File f = new File(projDir + File.separator + newPath);
Log.w("myApp", "cannot compress image");
try {
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fo != null) {
fo.flush();
fo.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources