Android based implementation saves images locally and displays them in albums

  • 2020-12-05 17:22:06
  • OfStack

App is becoming more and more user-friendly, with beautiful interface, diversified services and convenient operation. For example, when we were using app, we found that if we want to save the pictures above to the phone, we just need to click the save button provided in app at 1. So how do you save this image locally?

It's easy to save images as follows:


/**  First of all, the default file saving path  */
private static final String SAVE_PIC_PATH=Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : /mnt/sdcard;// Save to SD card 
private static final String SAVE_REAL_PATH = SAVE_PIC_PATH+ /good/savePic;// The exact location of the store 

Here's how to save it. Just pass in the parameters:


public static void saveFile(Bitmap bm, String fileName, String path) throws IOException {
String subForder = SAVE_REAL_PATH + path;
File foder = new File(subForder);
if (!foder.exists()) {
foder.mkdirs();
}
File myCaptureFile = new File(subForder, fileName);
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}www.ofstack.com
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}

This is good to save, but sometimes clearly saved down, why can't you see when you enter the album? Anyway, I have encountered such a problem. After saving successfully, we still need to send a system broadcast to inform the phone that there is a picture update. The broadcast is as follows:


Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);// The purpose of this broadcast is to update the gallery, send this broadcast into the album can find your saved pictures! Remember to send your updates file oh 

The above content is based on the Android implementation to save images to the local and can be displayed in the album, I hope to help you.


Related articles: