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();
}
Related
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();
}
I am trying to save bitmap to sd cart, but it saved like black image.
This code shows like I am creating bitmap
mStoryView.setDrawingCacheEnabled(true);
mStoryView.buildDrawingCache();
Bitmap result = Bitmap.createBitmap(mStoryView.getDrawingCache());
mStoryView.setDrawingCacheEnabled(false);
result = ImageUtil.saveStoryImage(this,result);
On this step bitmap looks good ( I set it on background view).
public static Bitmap saveStoryImage(Context context, Bitmap result) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "test.png");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
boolean b = result.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
After that I go to my sd card ondevice and saw black image.
What is the problem?
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();
}
}
}
When I take a picture with my Android camera, there is an image (imageview3) on my screen that I want to save with my picture.
Here is my onPictureTaken method
public void onPictureTaken(byte[] data, Camera camera) {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker");
imagesFolder.mkdirs();
String fileName = "Ker_.jpg";
output = new File(imagesFolder, fileName);
ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
b.compress(CompressFormat.JPEG, 95, fos);
try {
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
camera.stopPreview();
}
When I open the folder, the picture saved is only the imageview3 with a black background. Why has the real camera view not been saved?
EDIT
I'm trying something with canvas too:
output = new File(imagesFolder, fileName);
ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
fos.write(data);
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos2 = null;
b.compress(CompressFormat.JPEG, 95, fos2);
try {
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD());
Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap2, null, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Is that correct? How to save the canvas into a file on my sdcard (ie write data from canvas on fileoutputstream)
You're appending the JPEG of the imageview and the JPEG from the camera into a single file.
Create a separate file output stream for each file, and write the data from Bitmap.compress() and the onPictureTaken data array into their own streams.
If you want the two images to be combined into a single image, you'll need to decode the data array into a Bitmap, and then use a Canvas to draw the ImageView bitmap and the camera-captured Bitmap onto the canvas in the arrangement you want, and then save that out as a single jpeg.
You can't simply concatenate two compressed JPEG bitstreams together; the file format doesn't work that way.
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");