Example of saving picture to album function implemented by Android

Time:2020-2-8

The example of this article describes the function of saving pictures to albums developed and implemented by Android. To share with you for your reference, as follows:

If you don’t talk much, the effect is first:

Click the picture to callSavebitmapfromview (view view) of savephoto;Method to save pictures automatically

//Click the picture to save
imageView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    String[] PERMISSIONS = {
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE" };
    //Check whether there is write permission
    int permission = ContextCompat.checkSelfPermission(MainActivity.this,
        "android.permission.WRITE_EXTERNAL_STORAGE");
    if (permission != PackageManager.PERMISSION_GRANTED) {
      //There is no permission to write. To apply for the permission to write, a dialog box will pop up
      ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS,1);
    }
    try {
      //Create the savephoto class to save the picture
      SavePhoto savePhoto = new SavePhoto(MainActivity.this);
      savePhoto.SaveBitmapFromView(imageView);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
});

Specific implementation of savephoto class

Reference from: https://www.jb51.net/article/158635.htm

But some of the original blogger’s codes are wrong. I slightly modified them

public class SavePhoto{
  //Save the activity calling this class
  Context context;
  public SavePhoto(Context context) {
    this.context = context;
  }
  //How to save a file:
  public void SaveBitmapFromView(View view) throws ParseException {
    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);
    //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,bmp.toString() + ".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
      Log.v("qwe","002");
      fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/"+bitName ;
    }
    file = new File(fileName);
    if(file.exists()){
      file.delete();
    }
    FileOutputStream out;
    try{
      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))
      {
        out.flush();
        out.close();
        //Insert Gallery
        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), bitName, null);
      }
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    //Send a broadcast to refresh the display of the gallery
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));
  }
}

Layout in other parts (GridView selects pictures, etc.) code:

https://www.jb51.net/article/158666.htm

**Also attached: * * how to select pictures from album

//www.jb51.net/article/158640.htm

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.

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]