How to AchartEngine view convert image - android

I am draw pie graph using achartEngine library .How to Pie graph convert as images and store in sdcard.
GraphicalView mChartview;
// after draw pie graph....
Bitmap bitmap =mChartview.toBitmap()
String fileName = "test" + ".png";
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
FileOutputStream output = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, output);
But create 0 size image problem.

You can use following code for capturing,Here mChart is your GraphicalView
Bitmap bitmap1;
mChart.setDrawingCacheEnabled(true);
bitmap1 = Bitmap.createBitmap(mChart.getDrawingCache());
mChart.setDrawingCacheEnabled(false);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/DCIM/Camera");
myDir.mkdirs();
String fname = "Image123.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap1.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Related

Bitmap not saving in application folder in android nougat

i am working on bitmap save to application folder in android
below nougat its work fine but in nougat i have issue so can anyone help me?
below my code for save bitmap
String getRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDir = new File(getRoot + "/" + folderNAme);
String fname = picName + format;
File file = new File(myDir, fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(form, quality, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
act.sendBroadcast(mediaScanIntent);
return file;
Thanks
Please try this:-
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "filename.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
You have to give runtime permissions for nougat for saving your file.
See this:
https://developer.android.com/about/versions/nougat/android-7.0-changes.html

How to save image in external storage from bitmap in android

I am using ACTION_IMAGE_CAPTURE to capture image using camera. It works fine, But the problem is that image is showing in imageview after clicking but can not saved in external or internal storage. Here is my code for saving image in external storage.
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DashBoard/");
file.mkdirs();
ticket = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DashBoard/Ticket.jpg";
file4 = new File(file, ticket);
try {
FileOutputStream out = new FileOutputStream(file4);
bitmap.compress(Bitmap.CompressFormat.JPEG , 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
ivTicket.setImageBitmap(bitmap);
Any solution ?
Use ImageWorker Library for easily saving bitmaps/drawables/base64.
How to Save
ImageWorker.to(context).
directory("ImageWorker").
subDirectory("SubDirectory").
setFileName("Image").
withExtension(Extension.PNG).
save(sourceBitmap,85)
Easy as that. Contributions are welcomed.
Follow it -
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
EDIT: By using this line you will be able to see saved images in the gallery view.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Reference
Try This:
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
another example here
UPDATE
instead of Environment.getExternalStorageDirectory() you can use your storage location as API level 30 and above Storage-policy is changed.

Unable to save Bitmap Image

Hi I Am Trying to save my new bitmap image after applying filter into the SD-card or gallery. I am using this Code to save my bitmap that I have found on Stack overflow as well, but this code is not working for me
public void saveImage(View view) {
/*MediaStore.Images.Media.insertImage(getContentResolver(),newBitmap,"Image-Name","description");*/
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this,"Image Saved In SD Card",Toast.LENGTH_LONG).show();
}
Please let me know If I am doing any thing wrong?
Thanks in advance.
Make sure that you have added permission to write to sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

How to save an image from ImageView?

have imagview want to save it to memory here is my code :
View content = findViewById(R.id.full_image_view);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(root);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
after saving nothing happens and no image is exist ?
Try this..
Change root.createNewFile(); to cachePath.createNewFile();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
EDIT:
FileOutputStream ostream = new FileOutputStream(cachePath);
Use this function to save in SD card:
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1 - you need an appropriate permission in amnifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2- out.flush() check the out is not null..
3 -
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourforlderName";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "yourforlderName" + image + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
if you don't create directory in your sdcard then how to store images in sdcard of specific location?
so please check this...i hope its useful to you.
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) {
}
add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How about using following snippet ?
Drawable d = imageView.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext");
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

Questions reagarding bitmap image and image storage

I have the bitmap image that I got from my camera activity. Can someone please guide me as to how can I store this image in the gallery?
code:
In my button OnClickListener
Intent campic=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(campic,cameradata );
In my onActivityResult
if(resultCode==RESULT_OK)
{
Bundle bun=data.getExtras();
bmp=(Bitmap)bun.get("data");
SaveIamge(bmp);
iveventpic.setImageBitmap(bmp);
}
call this function to save bitmap in sdcard:
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
By calling this line u have to store that image in the gallery:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
and Add permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, title, desc);
As seen in this post.

Categories

Resources