Android development to save pictures to mobile album


The example of this article describes the Android development to save pictures to mobile album. To share with you for your reference, as follows:

There is a very common demand, when saving pictures, customers need to see that picture in the album. Sometimes it’s saved successfully (I / O stream writes pictures to sdcard), but I can’t see the picture when I open the album. I need to find the picture in the file management software. I found many articles on the Internet. It seems that I can’t save the picture in the album. This should be the reason for the mobile phone brand. Some brands of mobile phones can be displayed in the album, some brands of mobile phones can be displayed in the album But not. To solve this problem, the simplest and rudimentary way is to take a picture with that mobile phone, find it, check its path details, write it directly with IO stream according to the path, Android SDK build.brand The variable is the current mobile phone brand, and compatibility processing is done according to different brands. If the code of this article is not displayed in the album, you can do compatibility with this idea. In addition, it is worth mentioning that the image format needs to be JPEG to be displayed in the album, and the photos we take are also JPEG. The following uses the code to realize the above idea.

Since the current version of mobile phones is generally Android 6.0 or above, reading and writing external storage files requires dynamic permission application. This part of code can be written in the activity that currently needs to read and write external storage files.

String[] PERMISSIONS = {
    "android.permission.WRITE_EXTERNAL_STORAGE" };
//Check whether there is write permission
int permission = ContextCompat.checkSelfPermission(this,
if (permission != PackageManager.PERMISSION_GRANTED) {
  //There is no write permission. To apply for write permission, a dialog box will pop up
  ActivityCompat.requestPermissions(this, PERMISSIONS,1);
How to save a file:
public void SaveBitmapFromView(View view) {
  int w = view.getWidth();
  int h = view.getHeight();
  Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(bmp);
  view.layout(0, 0, w, h);
 //Shrink picture
 Matrix matrix = new Matrix();
 Matrix. Postscale (0.5f, 0.5f); // the ratio of length and width to zoom in and out
 bmp = Bitmap.createBitmap(bmp,0,0,        bmp.getWidth(),bmp.getHeight(),matrix,true);
 DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); 
  saveBitmap(bmp,format.format(new Date())+".JPEG");
 *Save the file named current date
Public void saveBitmap(Bitmap bitmap, String bitName){
    String fileName ;
    File file ;
    If (build. Brand. Equals ("Xiaomi") {// Xiaomi Mobile 
      fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/"+bitName ;
    }else{ // Meizu 、Oppo
      fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/"+bitName ;
    file = new File(fileName);
    FileOutputStream out;
      out = new FileOutputStream(file);
 //The format is JPEG, the picture taken by the camera is JPEG, and PNG cannot be displayed in the album
      if(bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out))
//Insert Gallery  
         MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), bitName, null);
    catch (FileNotFoundException e)
    catch (IOException e)
 //Send a broadcast to refresh the display of the gallery
    this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));

The above is the way to save pictures to albums. The code is written in the activity class. We just need to encapsulate the code into your imageutil or fileutil class a little bit, so you can easily apply it to your projects. I won’t encapsulate it here.

For more information about Android related content, readers can view our special topics: summary of Android graphics and image processing skills, introduction and advanced course of Android development, summary of Android debugging skills and solutions to common problems, summary of usage of Android basic components, summary of Android view skills, summary of Android layout skills and an Droid Control Usage Summary

I hope this article is helpful for Android programming.