当前位置:网站首页>Short video with goods source code, save pictures to photo album / Gallery

Short video with goods source code, save pictures to photo album / Gallery

2022-06-22 17:36:00 Yunbao network technology

Short video with goods source code , Save pictures to album / Gallery
One 、 describe
APP Users need to manually save pictures to mobile photo albums / Gallery
notes :Android11 The test works

Two 、 Realization
Just call the following method

  /* *  The picture  bitmap Save to gallery  */
    public static void saveBitmap(Context activity,Bitmap bitmap) {
    
        // because xml Using the background , So here is the background 
// To obtain parameters Bitmap Mode one : Bitmap bitmap=((BitmapDrawable)(imageView.getBackground())).getBitmap();
// To obtain parameters Bitmap Mode two : Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
 
        //t Set picture name , Keep png, The suffix here is png, Keep jpg, Just use the suffix jpg
        String imageName = System.currentTimeMillis() + "code.png";
 
        // create a file , The low version of Android 
       // File file=new File(Environment.getExternalStorageDirectory() +"/test.png");
 
        //Android Q 10 Each application is provided with a separate storage sandbox for external storage devices , No other app can directly access your app's Sandbox file 
        File f = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File file = new File(f.getPath() + "/"+imageName);// create a file 
      // file.getParentFile().mkdirs();
        try {
    
            // File output stream 
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            // The compressed image , If you want to save png, Just use Bitmap.CompressFormat.PNG, Keep jpg Just use Bitmap.CompressFormat.JPEG, Quality is 100%, Means no compression 
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            // write in , There will be a Caton , Because the picture is big 
            fileOutputStream.flush();
            // Remember to close the write stream 
            fileOutputStream.close();
            // Tips for success , After writing successfully , Please find the saved pictures in the corresponding directory 
            Log.e(" Write successfully ! Location directory ", f.getPath() + "/"+imageName);
 
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
            // Tips for failure , there Toast Tool class , You can use your own in the project , Delete... If not needed 
            ToastUtil.showToast(e.getMessage());
 
        } catch (IOException e) {
    
            e.printStackTrace();
            // Tips for failure 
            ToastUtil.showToast(e.getMessage());
        }
 
        //  The following steps must have , Otherwise, I can't find any pictures in the album , If you don't need to let the user know that you saved the picture , You don't have to write the following code .
        //  Insert the file into the system gallery 
        try {
    
            MediaStore.Images.Media.insertImage(activity.getContentResolver(),
                    file.getAbsolutePath(), imageName, null);
            ToastUtil.showToast( " Saved successfully , Please come   Photo album / Gallery   View in ");
        } catch (FileNotFoundException e) {
    
            ToastUtil.showToast( " Save failed ");
            e.printStackTrace();
        }
        //  Finally, notify the gallery to update 
        activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(new File(file.getPath()))));
 
    }

Note: Won't get pictures Bitmap Parameter students can refer to the following methods :
( Below imageView Is in your layout file ImageView Of id)
1) Mode one : If your picture comes from the Internet , The url It has been shown in the form of pictures in app Yes , When you need to save this picture , Convert picture to Bitmap

Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

2) Mode two : If your picture is app Local pictures , When you need to save this picture , Convert picture to Bitmap
if ImageView The properties of are set android:background=“@drawable/qr_code”, Use the following method

Bitmap bitmap=((BitmapDrawable)(imageView.getBackground())).getBitmap();

if ImageView The properties of are set android:src=“@drawable/qr_code”, Use the following method

 Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

The above is the source code of short video with goods , Save pictures to album / Gallery , More content welcome to follow the article

原网站

版权声明
本文为[Yunbao network technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221548003945.html