SoFunction
Updated on 2025-04-05

Android development realizes the function of saving pictures to mobile phone albums

This article describes the Android development function to save pictures to mobile phone albums. Share it for your reference, as follows:

There is a very common requirement. When saving pictures, customers need to see the picture in the album. Sometimes it is indeed saved successfully (the image is written to SDCard through IO stream), but when you open the album, you can't see the picture. You need to find the picture on the file management software. You have found many articles online, but it seems that you can't save the album. This should be the reason for the mobile phone brand. Some brands of mobile phones can be displayed in the album, while some brands of mobile phones cannot. The easiest and rough way to solve this problem is to take a photo with that phone, then find it, view its path details, and write it directly using IO streams according to the path. The variables of the Android SDK are the current mobile phone brand, and it is compatible according to different brands. If a reader uses the code of this article, it still cannot be displayed in the album. You can make compatibility based on this idea. It is also worth mentioning that the image format needs to be in JPEG format to be displayed in the album, and the photos we take are also in JPEG format. The following is the code to implement the above idea.

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

String[] PERMISSIONS = {
    ".READ_EXTERNAL_STORAGE",
    ".WRITE_EXTERNAL_STORAGE" };
//Check whether there is permission to writeint permission = (this,
    ".WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
  // If you do not have the permission to write, if you apply for the permission to write, a dialog box will pop up  (this, PERMISSIONS,1);
}
How to save files:
public void SaveBitmapFromView(View view) {
  int w = ();
  int h = ();
  Bitmap bmp = (w, h, .ARGB_8888);
  Canvas c = new Canvas(bmp);
  (0, 0, w, h);
  (c);
 // Shrink the picture Matrix matrix = new Matrix();
 (0.5f,0.5f); //The ratio of length and width enlargement and reduction bmp = (bmp,0,0,        (),(),matrix,true);
 DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); 
  saveBitmap(bmp,(new Date())+".JPEG");
}
/*
  * Save the file, the file name is the current date
  */
Public void saveBitmap(Bitmap bitmap, String bitName){
    String fileName ;
    File file ;
    if( .equals("Xiaomi") ){ // Xiaomi mobile phone      fileName = ().getPath()+"/DCIM/Camera/"+bitName ;
    }else{ // Meizu 、Oppo
      fileName = ().getPath()+"/DCIM/"+bitName ;
    }
    file = new File(fileName);
    if(()){
      ();
    }
    FileOutputStream out;
    try{
      out = new FileOutputStream(file);
 // The format is JPEG, the pictures taken by the camera are in JPEG format, and the PNG format cannot be displayed in the album      if((, 90, out))
      {
        ();
        ();
// Insert the gallery         ((), (), bitName, null);
      }
    }
    catch (FileNotFoundException e)
    {
      ();
    }
    catch (IOException e)
    {
      ();
    }
 // Send a broadcast and notify refreshing the display of the gallery    (new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, ("file://" + fileName)));
}

The above is how to save pictures to the album. The code is written in the Activity class. As long as we wrap these codes a little into your ImageUtil or FileUtil classes, you can easily apply them to your project. I won't wrap them here.

For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.