Unable to save screenshot in gallery - android

I,m trying to capture a snapshot of an android activity, but unable to save it in gallery.
the screenshot turns black
I'm also trying to convert this snapshot into a pdf file..Plz help..
This is my code..
public static void saveImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytes);
File f = new File("sdcard/camera_app/test.png");
//File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

To register an image into the gallery:
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

Related

How to access saved bitmap as a file on android?

I am saving a Bitmap (bitmap) to a file as follows:
String fileName = "image.jpg";
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName,
Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
How to access the saved image as a file? Thanks.
Try this.
String fileName = "image.jpg";
try {
String path = Environment.getExternalStorageDirectory().toString(); //path where the file will be stored.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(path,fileName); // the File to save , append
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
don't forget to add Storage Permission and get the permission on runtime as well.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Phone reboots when image file is tried to open from application 2nd time in 23 sdk version?

I Just created an application which takes the screenshot of the page when a button is clicked and after saving to folder the snackbar with open icon shows. When i press the open, the image is opened but not in the gallery, as if it is opened from a file manager! But till then its okay! Again if i press button this time the phone just restarts. weird!! It does not have any issue upto lollypop!! But this happens only in Android Marshmallow!!
my Code to save screenshot and open the image is:
private void temp(){
RelativeLayout parent = (RelativeLayout) findViewById(R.id.printLayout);// which includes your view or buttons?
parent.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(parent.getDrawingCache());
parent.setDrawingCacheEnabled(false);
try {
final File f = savebitmap(bitmap);
Log.e("File Loc",f.toString());
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Image saved successfully in mobeeload folder", Snackbar.LENGTH_LONG)
.setAction("OPEN", new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + f.getAbsolutePath());
intent.setDataAndType(uri,"image/*");
startActivity(intent);
}
});
snackbar.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MobeeLoad/testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}
---------UPDATE------------
I found that the image is opened in same application (my App) and not in any gallery app in my phone!!
Focusing on your comment -
It happens when trying to save the image back to back
Must be a memory problem, Try to recycle your bitmap after writing it into a disk. Here is how you can do that -
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MobeeLoad/testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
bmp.recycle();
return f;
}

Dynamically created image file cannot be found in Gallery or image picker in Android

I am developing an Android app. In my app, I am creating image file from bitmap then save it to device. The process of creating image file from bitmap and save image to device is okay. The problem is I cannot find that created image in gallery. But the file really exist when I search it from file manager.
Here is my code:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists()){
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
As you can see, I save it in the picture folder. I tried save it in download folder as well. File saved to device successfully but the problem is image is not displayed in Gallery. But exist in file manager. How can I make that image searchable in gallery?
try this:
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if(file.exists())
{
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
template.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Toast.makeText(context,"Templated saved to your device",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
}
Use this code after saving file
try {
MediaScannerConnection.scanFile(context,
new String[] { mPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
}

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

MODE_WORLD_WRITEABLE is deprecated?

File f = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
f.delete();
//Create new file
FileOutputStream fos = MainActivity.appContext.openFileOutput("Captured.jpg", Context
.MODE_WORLD_WRITEABLE);
//FileOutputStream out = new FileOutputStream(file);
fos.close();
//Get reference to the file
filezz = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
as MODE_WORLD_WRITEABLE is deprecated and i don't want to go with that code so
can i get the replcement of above code ?
Replace Context.MODE_WORLD_WRITEABLE with 0 or Context.MODE_PRIVATE.
If you are doing this from bitmap then use below code
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), "Captured.jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources