Android Development and Realization of Saving Pictures to Mobile Phone Photo Album

  • 2021-11-02 02:40:56
  • OfStack

This paper describes the development and realization of Android to save pictures to mobile phone photo albums. Share it for your reference, as follows:

There is a very common requirement that when saving a picture, the customer needs to see the picture in the photo album. Sometimes it does succeed (the picture is written to SDCard through IO stream), However, I can't see the picture when I open the photo album. I need to find the picture on the file management software. I have found many articles on the Internet, but it seems that I can't save the photo album. This should be the reason for the mobile phone brand. Some brands of mobile phones can be displayed in the photo album, while others can't. To solve this problem, The simplest and rudest way is, Take a photo with that mobile phone, then find it, check its path details, and write it directly according to the path with IO stream. The Build.BRAND variable of Android SDK is the brand of the current mobile phone, which is compatible according to different brands. If some readers use the code of this article and still can't be displayed in the photo album, they can do compatibility with this idea. In addition, it is worth mentioning that the picture format needs to be JPEG format to be displayed in the photo album, and the photos we took are also JPEG format. The following code is used to realize the above idea.

In view of the fact that the current mobile phone version is generally above Android 6.0, it is necessary to dynamically apply for permission to read and write external storage files. This part of the code can be written in Activity, which currently needs to read and write external storage files.


String[] PERMISSIONS = {
    "android.permission.READ_EXTERNAL_STORAGE",
    "android.permission.WRITE_EXTERNAL_STORAGE" };
// Check whether you have write permission 
int permission = ContextCompat.checkSelfPermission(this,
    "android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
  //  If you don't have permission to write, apply for permission to write, and a dialog box will pop up 
  ActivityCompat.requestPermissions(this, PERMISSIONS,1);
}
 How to save the 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);
  view.draw(c);
 //  Zoom out a picture 
 Matrix matrix = new Matrix();
 matrix.postScale(0.5f,0.5f); // The ratio of enlargement and reduction of length and width 
 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 with the current date 
 */
Public void saveBitmap(Bitmap bitmap, String bitName){
    String fileName ;
    File file ;
    if(Build.BRAND .equals("Xiaomi") ){ //  Millet mobile phone  
      fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/"+bitName ;
    }else{ // Meizu  , Oppo
      fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/"+bitName ;
    }
    file = new File(fileName);
    if(file.exists()){
      file.delete();
    }
    FileOutputStream out;
    try{
      out = new FileOutputStream(file);
 //  Format is  JPEG The picture taken by the camera is JPEG Format, PNG Format cannot be displayed in photo albums 
      if(bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out))
      {
        out.flush();
        out.close();
//  Insert Gallery   
         MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), bitName, null);
      }
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
 //  Send a broadcast notifying you 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 photo albums. The code is written in Activity class, but we only need to encapsulate these codes into your ImageUtil or FileUtil class slightly, and you can easily apply them to your project. I won't encapsulate them here.

More readers interested in Android can check the topics on this site: "Summary of Android Graphics and Image Processing Skills", "Introduction and Advanced Tutorial of Android Development", "Summary of Android Debugging Skills and Common Problem Solutions", "Summary of Android Basic Component Usage", "Summary of Android View View Skills", "Summary of layout Layout layout Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: