Android saving images in application context - java

In my MainActivity onCreate() I want to populate a listview. This listview contains Strings and Bitmaps.
What's the best way to save these data to get them back when the application is restarted ?

There is my own cache solution:
public class MyCache {
private String DiretoryName;
public void OpenOrCreateCache(Context _context, String _directoryName){
File file = new File(_context.getCacheDir().getAbsolutePath() + "/" + _directoryName);
if(!file.exists()){
file.mkdir();
}
DiretoryName = file.getAbsolutePath();
}
public void SaveBitmap(String fileName, Bitmap bmp){
try {
File file = new File(DiretoryName+ "/" + fileName);
if(file.exists()){
file.delete();
}
FileOutputStream Filestream = new FileOutputStream(DiretoryName + "/" + fileName);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Filestream.write(byteArray);
Filestream.close();
bmp.recycle();
}
catch (Exception e){
e.printStackTrace();
}
}
public Bitmap OpenBitmap(String name){
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
File file = new File(DiretoryName+ "/" + name);
if(file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(DiretoryName+ "/" + name, options);
return bitmap;
}
else{
return null;
}
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
public void CleanCacheBitMap(){
File file = new File(Diretorio);
if(file.exists()){
file.delete();
}
}
}
And onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
cache = new MyCache();
cache.OpenOrCreateCache(this, "TheFolderNameForOpenOrSaveInAppCache");
}
And for save on runtime:
cache.SaveBitmap("BitMapName", YourBitmap);
And openning on runtime:
Bitmap bmp = cache.OpenBitmap("BitMapName");
This solution save any bitmap, in particular folder in your app cache (internal storage).

There are different possibilities depending on how tightly you want to hang on to these files. The most reliable is the method getFilesDir(). Each application has its own private "files directory".
All the details are here:
https://developer.android.com/training/basics/data-storage/files.html
The cache directory (getCacheDir()) is similar but the user can erase the cache contents so that's more for temporary files you don't want to hang onto.
Strings and bitmaps can both be saved to files. You can create your own name value mapping file to link them. Or use JSON objects, or many other things to organize your list of strings/bitmaps.

Related

RelativeLayout screenshot including mapFragment saves black image

I have a RelativeLayout view which includes some TextViews and a map fragment. I want to take a screenshot of the whole screen (including the map as displayed and the text) and save it. Everything working as expected but the view with the map shows a black image. How is this possible?
I've also tried with some delay interval to be sure the map is fully loaded but didn't worked.
Bitmap bitmap = takeScreenshot();
createDirectoryAndSaveFile(bitmap,intent.getStringExtra("TEST"));
public Bitmap takeScreenshot() {
View rootView = findViewById(R.id.relative_layout);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
private void createDirectoryAndSaveFile(Bitmap bitmap, String name) {
File folder = new File(Environment.getExternalStorageDirectory() +"/Test");
if (!folder.exists()) {
File screenshoDir = new File(Environment.getExternalStorageDirectory() + "/Test/");
screenshoDir.mkdirs();
}
File file = new File(new File(Environment.getExternalStorageDirectory() + "/Test/"), name);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
} catch (Exception e) {
e.printStackTrace();
}
}

Saving Image from an Image-View to device

I have two Image-View in a layout, one as at background and another is above that(at foreground) and I want to save both the images. So can anyone help me out that how can I save both the images into the device storage as single image.
Thank you.
A simple solution I found here is to put both of your imageView in a single Layout and then save your layout as a Bitmap. I will retype the solution code here
private Bitmap getBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Toast.makeText(StopWarApp.getContext(), "Something went wrong",
Toast.LENGTH_SHORT).show();
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
Now that you have your Bitmap, you can save it to your storage like this
private void saveImage(Bitmap finalBitmap, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image-" + image_name+ ".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();
}
}
Don't forget to add your permissions in the manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Taking screenshot and saving into app's own directory

I know this question has been answered, but I would like to get a better explanation as I have tried implementing it but it doesn't seem to work.
I have the following code :
private void takeScreenshot() {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
//Get screenshot
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
Date fileName = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", fileName);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File image = new File(directory,fileName+".jpg");
try {
FileOutputStream fos = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
}
}
What I would like to happen is to take a screenshot of the screen, save it to a folder with my app name, and have it be readable by the android phone's gallery. My code does none of the above. I do not see any folder w/ the name of my app when I use file explorer, and it doesn't appear in the gallery as well. It seems it doesn't even save the image. Can you please tell me what is wrong with my code?
The code below creates a directory called "AppName" and then stores the screenshot in that directory. This will be readable by the gallery as well. Your code (and the code below) will not work if you do not have the WRITE_EXTERNAL_STORAGE permission.
private static File getOutputMediaFile() {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp"); //change to your app name
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
return mediaFile;
}
private void takeScreenshot(){
//Get screenshot
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
Log.d(TAG, "error creating media file, check storage permission");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
bitmap.recycle();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
Ensure to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to your manifest and ask for permissions on runtime with
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
00);
The code finds and/or creates a directory with the app name in the getOutputMediaFile() method, then returns a file in the directory with timestamp as its name. Then in the takeScreenshot() method, the screenshot bitmap is converted to a byte[] and a fileOutputStream is used to write this byte[] to the file returned by getOutputMediaFile().
The result is a screenshot saved to the gallery in the directory "MyCameraApp" (Change to whatever your app's name is)
Hope this helps!

Android App saving to SD Card

I'm having some trouble. I'm new to java and Android programming. I'm using a template to get started and I'm stuck.
I have a app that pulls images from my Tumblr Feed and presents them on the screen with a download button. It works fine but installs to the root of the internal storage. How do I save it to a folder in the internal storage called "/Pictures/Tumblr"?
My code is:
public void onLoadingComplete(final String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
// close button click event
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "tumblr_"+images.get(position).getId()+".jpg");
try {
fOut = new FileOutputStream(file);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
String saved = getResources().getString(R.string.saved);
Toast.makeText(getBaseContext(), saved + " " + file.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
I've tried changing the
File file = new File(path, "tumblr_"+images.get(position).getId()+".jpg");
To
File file = new File(path+"/Pictures/Tumblr", "tumblr_"+images.get(position).getId()+".jpg");
But I know it's wrong.
Can anyone help?

android - save image into gallery

i have an app with a gallery of images and i want that the user can save it into his own gallery.
I've created an option menu with a single voice "save" to allow that but the problem is...how can i save the image into the gallery?
this is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuFinale:
imgView.setDrawingCacheEnabled(true);
Bitmap bitmap = imgView.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i'm not sure of this part of code:
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
is it correct to save into the gallery?
unfortunately the code doesn't work :(
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
The former code will add the image at the end of the gallery. If you want to modify the date so it appears at the beginning or any other metadata, see the code below (Cortesy of S-K, samkirton):
https://gist.github.com/samkirton/0242ba81d7ca00b475b9
/**
* Android internals have been modified to store images in the media folder with
* the correct date meta data
* #author samuelkirton
*/
public class CapturePhotoUtils {
/**
* A copy of the Android internals insertImage method, this method populates the
* meta data with DATE_ADDED and DATE_TAKEN. This fixes a common problem where media
* that is inserted manually gets saved at the end of the gallery (because date is not populated).
* #see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String, String)
*/
public static final String insertImage(ContentResolver cr,
Bitmap source,
String title,
String description) {
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, title);
values.put(Images.Media.DISPLAY_NAME, title);
values.put(Images.Media.DESCRIPTION, description);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
// Add the date meta data to ensure the image is added at the front of the gallery
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
Uri url = null;
String stringUrl = null; /* value to be returned */
try {
url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (source != null) {
OutputStream imageOut = cr.openOutputStream(url);
try {
source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
} finally {
imageOut.close();
}
long id = ContentUris.parseId(url);
// Wait until MINI_KIND thumbnail is generated.
Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null);
// This is for backward compatibility.
storeThumbnail(cr, miniThumb, id, 50F, 50F,Images.Thumbnails.MICRO_KIND);
} else {
cr.delete(url, null, null);
url = null;
}
} catch (Exception e) {
if (url != null) {
cr.delete(url, null, null);
url = null;
}
}
if (url != null) {
stringUrl = url.toString();
}
return stringUrl;
}
/**
* A copy of the Android internals StoreThumbnail method, it used with the insertImage to
* populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
* meta data. The StoreThumbnail method is private so it must be duplicated here.
* #see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
*/
private static final Bitmap storeThumbnail(
ContentResolver cr,
Bitmap source,
long id,
float width,
float height,
int kind) {
// create the matrix to scale it
Matrix matrix = new Matrix();
float scaleX = width / source.getWidth();
float scaleY = height / source.getHeight();
matrix.setScale(scaleX, scaleY);
Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
source.getWidth(),
source.getHeight(), matrix,
true
);
ContentValues values = new ContentValues(4);
values.put(Images.Thumbnails.KIND,kind);
values.put(Images.Thumbnails.IMAGE_ID,(int)id);
values.put(Images.Thumbnails.HEIGHT,thumb.getHeight());
values.put(Images.Thumbnails.WIDTH,thumb.getWidth());
Uri url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values);
try {
OutputStream thumbOut = cr.openOutputStream(url);
thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
thumbOut.close();
return thumb;
} catch (FileNotFoundException ex) {
return null;
} catch (IOException ex) {
return null;
}
}
}
Actually, you can save you picture at any place. If you want to save in a public space, so any other application can access, use this code:
storageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
),
getAlbumName()
);
The picture doesn't go to the album. To do this, you need to call a scan:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
You can found more info at https://developer.android.com/training/camera/photobasics.html#TaskGallery
I've tried a lot of things to let this work on Marshmallow and Lollipop.
Finally i ended up moving the saved picture to the DCIM folder (new Google Photo app scan images only if they are inside this folder apparently)
public static File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(System.currentTimeInMillis());
File storageDir = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/");
if (!storageDir.exists())
storageDir.mkdirs();
File image = File.createTempFile(
timeStamp, /* prefix */
".jpeg", /* suffix */
storageDir /* directory */
);
return image;
}
And then the standard code for scanning files which you can find in the Google Developers site too.
public static void addPicToGallery(Context context, String photoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(photoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
Please remember that this folder could not be present in every device in the world and that starting from Marshmallow (API 23), you need to request the permission to WRITE_EXTERNAL_STORAGE to the user.
According to this course, the correct way to do this is:
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
)
This will give you the root path for the gallery directory.
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
You can create a directory inside the camera folder and save the image. After that, you can simply perform your scan. It will instantly show your image in the gallery.
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()+ "/Camera/Your_Directory_Name";
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image-" + image_name + ".png";
File file = new File(myDir, fname);
System.out.println(file.getAbsolutePath());
if (file.exists()) file.delete();
Log.i("LOAD", root + fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
Here's what worked for me:
private fun saveBitmapAsImageToDevice(bitmap: Bitmap?) {
// Add a specific media item.
val resolver = this.contentResolver
val imageStorageAddress = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}
val imageDetails = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "my_app_${System.currentTimeMillis()}.jpg")
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis())
}
try {
// Save the image.
val contentUri: Uri? = resolver.insert(imageStorageAddress, imageDetails)
contentUri?.let { uri ->
// Don't leave an orphan entry in the MediaStore
if (bitmap == null) resolver.delete(contentUri, null, null)
val outputStream: OutputStream? = resolver.openOutputStream(uri)
outputStream?.let { outStream ->
val isBitmapCompressed =
bitmap?.compress(Bitmap.CompressFormat.JPEG, 95, outStream)
if (isBitmapCompressed == true) {
outStream.flush()
outStream.close()
}
} ?: throw IOException("Failed to get output stream.")
} ?: throw IOException("Failed to create new MediaStore record.")
} catch (e: IOException) {
throw e
}
}
Note: For Build.VERSION.SDK_INT < 29, the image has to be saved locally on disk first, which will increase the app size as the user saves more images. The user can delete the image later, in the Files app, but the local image has to sync with Google Photos or Amazon Photos in the cloud.
Saving the image to the cloud is accomplished by having the user open their Google Photos or Amazon Photos app after exporting and before deleting your app APK. If the user of < 29 deletes your APK before opening Google Photos or Amazon Photos, the photo will be lost.
This is a bug with Android Builds before Q (Level 29). Level 29 and later save directly to the Photo Library.
Android Manifest XML
<!-- Adding Read External Storage Permission -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Save Function
// - Save Image -
#Throws(FileNotFoundException::class)
private fun saveImage(
bitmap: Bitmap,
context: Context,
folderName: String
) {
if (Build.VERSION.SDK_INT >= 29) {
val values = ContentValues()
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/$folderName")
values.put(MediaStore.Images.Media.IS_PENDING, true)
// RELATIVE_PATH and IS_PENDING are introduced in API 29.
val uri: Uri? = context.contentResolver
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
if (uri != null) {
saveImageToStream(bitmap, context.contentResolver.openOutputStream(uri))
values.put(MediaStore.Images.Media.IS_PENDING, false)
context.contentResolver.update(uri, values, null, null)
}
} else {
var dir = File(
applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
""
)
// getExternalStorageDirectory is deprecated in API 29
if (!dir.exists()) {
dir.mkdirs()
}
val date = Date()
val fullFileName = "myFileName.jpeg"
val fileName = fullFileName?.substring(0, fullFileName.lastIndexOf("."))
val extension = fullFileName?.substring(fullFileName.lastIndexOf("."))
var imageFile = File(
dir.absolutePath
.toString() + File.separator
+ fileName + "_" + Timestamp(date.time).toString()
+ ".jpg"
)
println("imageFile: $imageFile")
saveImageToStream(bitmap, FileOutputStream(imageFile))
if (imageFile.getAbsolutePath() != null) {
val values = ContentValues()
values.put(MediaStore.Images.Media.DATA, imageFile.absolutePath)
// .DATA is deprecated in API 29
context.contentResolver
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
}
}
}
private fun contentValues(): ContentValues? {
val values = ContentValues()
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
return values
}
private fun saveImageToStream(bitmap: Bitmap, outputStream: OutputStream?) {
println("saveImageToStream")
if (outputStream != null) {
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.close()
// success dialog
runOnUiThread {
val successDialog = SuccessDialog.getInstance(null)
successDialog.show(supportFragmentManager, SuccessDialog.TAG)
}
} catch (e: Exception) {
e.printStackTrace()
// warning dialog
runOnUiThread {
val warningDialog = WarningDialog.getInstance(null)
warningDialog.show(supportFragmentManager, WarningDialog.TAG)
}
}
}
}
I come here with the same doubt but for Xamarin for Android, I have used the Sigrist answer to do this method after save my file:
private void UpdateGallery()
{
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Java.IO.File file = new Java.IO.File(_path);
Android.Net.Uri contentUri = Android.Net.Uri.FromFile(file);
mediaScanIntent.SetData(contentUri);
Application.Context.SendBroadcast(mediaScanIntent);
}
and it solved my problem, Thx Sigrist. I put it here becouse i did not found an answare about this for Xamarin and i hope it can help other people.
In my case the solutions above did not work I had to do the following:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(f)));
String filePath="/storage/emulated/0/DCIM"+app_name;
File dir=new File(filePath);
if(!dir.exists()){
dir.mkdir();
}
This code is in onCreate method.This code is for creating a directory of app_name.
Now,this directory can be accessed using default file manager app in android.
Use this string filePath wherever required to set your destination folder.
I am sure this method works on Android 7 too because I tested on it.Hence,it can work on other versions of android too.
Just you need to scan your Media After Saving finished.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File dir = new File(filepath.getAbsolutePath()+"/Pro Scanner/");
if(!dir.exists()){
dir.mkdir();
}
File file = new File(dir,System.currentTimeMillis()+"_Pro_Scanner.png");
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
downloadQRCode.setVisibility(View.VISIBLE);
loadingBar.setVisibility(View.INVISIBLE);
}
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
Toast.makeText(GenerateQRCodeActivity.this, "QR image saved successfully", Toast.LENGTH_SHORT).show();
try {
outputStream.flush();
outputStream.close();
loadingBar.setVisibility(View.INVISIBLE);
downloadDone.setVisibility(View.VISIBLE);
downloadDone.setAnimation(bottomAnimation);
} catch (IOException e) {
downloadQRCode.setVisibility(View.VISIBLE);
loadingBar.setVisibility(View.INVISIBLE);
e.printStackTrace();
}
MediaScannerConnection.scanFile(GenerateQRCodeActivity.this,new String[]{file.getPath()},new String[] {"image/jpeg"},null);
These code are same as everyone .If you try the bellow code after this it will work. You just need these one line of code:
MediaScannerConnection.scanFile(GenerateQRCodeActivity.this,new String[]{file.getPath()},new String[] {"image/jpeg"},null);
Boom!!! Yo can now get your saved image on your Gallery.

Categories