Android will be saved to a local Bitmap

Overview

The picture is a program can make you become more beautiful, so we will use the pictures in our software. But for the operation of the picture is more complex. Today, we learn about the case to save our image to our local.


Development environment

  • Android Studio 3.6
  • Android 11
  • Mac OS 10:15
  • Simulator Google Pixel3 API R

Then learn how to accomplish our function

According to international management, we first look at our code:

/**
 * Bitmap 帮助类之一
 */
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");
        //指定我们想要存储文件的地址
        String TargetPath = mContext.getFilesDir() + "/images/";
        Log.d("Save Bitmap", "Save Path=" + TargetPath);
        //判断指定文件夹的路径是否存在
        if (!FileUtils.fileIsExist(TargetPath)) {
            Log.d("Save Bitmap", "TargetPath isn't exist");
        } else {
            //如果指定文件夹创建成功,那么我们则需要进行图片存储操作
            File saveFile = new File(TargetPath, name);

            try {
                FileOutputStream saveImgOut = new FileOutputStream(saveFile);
                // compress - 压缩的意思
                bm.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
                //存储完成后需要清除相关的进程
                saveImgOut.flush();
                saveImgOut.close();
                Log.d("Save Bitmap", "The picture is save to your phone!");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

}

We write methods to complete, relatively simple.
Prime Minister, we need to get the root of our software. We can use Context.getFilesDir()to get to the root of the software, and I need to save the file to our images below.
So I get the storage path then what we need to determine is not the first time, or the directory exists, then we look at how to judge our file there is a method. Look at the source code:

class FileUtils {
    /**
     * 判断指定目录的文件夹是否存在,如果不存在则需要创建新的文件夹
     * @param fileName 指定目录
     * @return 返回创建结果 TRUE or FALSE
     */
    static  boolean fileIsExist(String fileName)
    {
        //传入指定的路径,然后判断路径是否存在
        File file=new File(fileName);
        if (file.exists())
            return  true;
        else{
            //file.mkdirs() 创建文件夹的意思
            return file.mkdirs();
        }
    }
}

We passed in the method specified storage path, and then determine whether there is, if there is we need to create our specified directory, and then return the results we create. So that our operations to the directory basically completed.

To complete the operation of the directory, we look at how to complete our storage process, we need to create a FileOutputStreamwrite to a picture, and we need the picture corresponding to the compression operation.
And we need to clear the last of our related methods, so that our operations to Bitmap basically completed. easier.

Guess you like

Origin www.cnblogs.com/cao-1/p/12508456.html