当前位置:网站首页>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
边栏推荐
- Review the executor from the perspective of application submission
- Qt笔记-QMap自定义键(key)
- Mysql-5.6.21-centos6.5 source code installation configuration
- 使用 POST 向 ASP.NET Core 传递数据时的长度限制与解决方案
- Redis实现延迟队列的正确姿势
- Qt Notes - qmap Custom key
- Mybaits: interface proxy implementation Dao
- WPF效果第一百九十篇之再耍ListBox
- Hydra installation and use
- mysql指令执行sql文件
猜你喜欢

Qt笔记-QMap自定义键(key)

Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)

Seeing the big from the small: a simple example of domain modeling, understanding "domain driven".

170million passwords of Netcom learning link have been leaked! What are the remedies?
![[face recognition] matlab simulation of face recognition based on googlenet deep learning network](/img/e8/050ca85542ccbf1402b84c5dbf6f5e.png)
[face recognition] matlab simulation of face recognition based on googlenet deep learning network

Recommend 7 super easy-to-use terminal tools - ssh+ftp

STM32 series (HAL Library) - f103c8t6 hardware SPI illuminates OLED screen with word library

Interview shock 58: Six differences among truncate, delete and drop!

【进阶自动化测试第一步】1分钟带你了解自动化测试

Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)
随机推荐
Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde
[recruitment] [Beijing Zhongguancun / remote] [tensorbase][open source data warehouse] and other people do one thing
0 basic how to get started software testing, can you succeed in changing careers?
Basic application of scala for
Post to asp Net core length limitation and solution when transferring data
一文带你掌握Tcpdump命令的使用
How to solve the problem of database?
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
Stop automatically after MySQL server starts
You call this crap high availability?
STM32 series (HAL Library) - f103c8t6 hardware SPI illuminates OLED screen with word library
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
0基础如何入门软件测试,能转行成功吗?
企业级软件开发新模式:低代码
短视频直播源码,EditText输入框的使用
WPF 实现星空效果
Qt Notes - qmap Custom key
Problems and recovery of spark streaming checkpoint
What is flush software? Is it safe to open a mobile account?
JSP learning (I) -- overview of JSP