Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Am developing a coloring of images in android. So after applying colors to my image when i click another imageview like save, then i have to save that image to gallery.
To get Bitmap from imageView:
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
To save it in a file:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
You have to
Save the image to your persistent storage.
Add an entry to the MediaStore content provider.
First one can be achieved using the following code:
FileOutputStream out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Second,
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);
First get the drawingCache(bitmap) of the imageView and then save the bitmap to the SDCard.
File folder = new File(Environment.getExternalStorageDirectory()+"/folder/");
if(!folder.exists()) folderAppointment.mkdirs();
try {
this.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache();
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
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 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();
}
I wrote code witch can to save image to sd card.my code working perfect but when i going to sd car with file system my image is public.this is my code
private Bitmap takeScreenShot(Context content, ScrollView view) {
int totalHeight = view.getChildAt(0).getHeight();
int totalWidth = view.getChildAt(0).getWidth();
Bitmap bitmap = getBitmapFromView(view, totalHeight, totalWidth);
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/image.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(content.getContentResolver(),
bitmap, "Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
how i can save imege in sd card to can hide or unpublish my image.i meean ,i don't need to can show it with file manager
if anyone knows solution please help me
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capture Image from Camera and Display in Activity
I`m new in android programation and I´m having a problem with some stuff.
I have an ImageView Without any image loaded, when I take a photo with the camera, the captured image puts into the ImageView. I want to save this, but I dont know how to save the directory of the photo in a string, and how to load this directory in the imageView at the "onCreate".
I know that this is a noob cuestion, but this is my first time with this stuff.
Thanks
This code will get your ImageView as a Bitmap
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
imgView.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(imgView.getDrawingCache());
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/your_app_package/image.png");
This code will save it to a file
try {
FileOutputStream out = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
I doing something like this:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 80, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + picId + ".jpg");
FileOutputStream fo = null;
try
{
f.createNewFile();
fo = new FileOutputStream(f);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
fo.write(bytes.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
}
...where picId is name for a file.
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");