The Android implementation saves Bitmap locally

  • 2021-11-24 03:02:28
  • OfStack

Overview

Pictures are a way to make your program more beautiful, so we will use pictures in our software. However, the operation of pictures is also complicated. Today, we learn how to save our pictures to our local area.

Development environment

Android Studio 3.6

Android 11

Mac OS 10.15

Simulator Google Pixel3 API R

Then learn how to complete our functions in 1

According to international practice, let's first look at our code:


/**
 * Bitmap  Help class 1
 */
class BitmapUtils {

  /**
   * Save Bitmap
   *
   * @param name file name
   * @param bm  picture to save
   */
  static void saveBitmap(String name, Bitmap bm, Context mContext) {
    Log.d("Save Bitmap", "Ready to save picture");
    // Specify the address where we want to store the file 
    String TargetPath = mContext.getFilesDir() + "/images/";
    Log.d("Save Bitmap", "Save Path=" + TargetPath);
    // Determines whether the path of the specified folder exists 
    if (!FileUtils.fileIsExist(TargetPath)) {
      Log.d("Save Bitmap", "TargetPath isn't exist");
    } else {
      // If the specified folder is created successfully, then we need to store pictures 
      File saveFile = new File(TargetPath, name);

      try {
        FileOutputStream saveImgOut = new FileOutputStream(saveFile);
        // compress -  The meaning of compression 
        bm.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
        // After the storage is completed, the related processes need to be cleared 
        saveImgOut.flush();
        saveImgOut.close();
        Log.d("Save Bitmap", "The picture is save to your phone!");
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

}

In this way, our writing method is completed, which is relatively simple.

Prime Minister, we need to get the root directory of our software. We can use Context.getFilesDir() To get the root directory of the software, and I need to save it to our images Below the file.

This way we get our storage path and then we need to determine whether it is the first time to use it or whether the directory exists. Then we will look at how to determine the existence of our files.

Look at the source code under 1:


class FileUtils {
  /**
   *  Determines whether the folder in the specified directory exists, and if it does not exist, it needs to create a new folder 
   * @param fileName  Specify a directory 
   * @return  Return the creation result  TRUE or FALSE
   */
  static boolean fileIsExist(String fileName)
  {
    // Pass in the specified path, and then determine whether the path exists 
    File file=new File(fileName);
    if (file.exists())
      return true;
    else{
      //file.mkdirs()  The meaning of creating a folder 
      return file.mkdirs();
    }
  }
}

We pass in the specified storage path in this method, then determine whether it exists, if so, we need to create our specified directory, and then return our creation results. In this way, our operation on the directory is basically completed.

To complete the operation on the directory, we are looking at how to complete our stored procedure. We need to create 1 FileOutputStream To be used for writing pictures, and we need to compress the pictures accordingly.

And we need to clear our relevant methods at the end, so that our land Bitmap The operation is basically completed. It's relatively simple.


Related articles: