Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of picture preview and saving with Android

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "Android how to achieve picture preview and save function" is not understood by most people, so the editor gives you a summary of the following contents, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article, let's take a look at this "Android how to achieve picture preview and save" article.

Introduce plug-in

First, we need to introduce two plug-ins from the powerful Flutter community, namely:

Photo_view: ^ 0.13.0 is used to load and view large images.

Image_gallery_saver: ^ 1.7.1 is used to save pictures locally.

First of all, let's take a look at how to view the large image, the use is very simple, the use of PhotoView only two lines of code to achieve picture magnification and reduction, support for local pictures and network pictures to view.

@ overrideWidget build (BuildContext context) {return Container (child: PhotoView (/ / imageProvider: AssetImage ("assets/xxx.jpg"), imageProvider: NetworkImage ("imageUrl"),);}

But this obviously does not meet our needs. Generally speaking, we need to see that the big picture is a list of pictures. See below:

Document translation:

/ / if you use the gallery list effect, use PhotoViewGallery

To show several images and let user change between them, use PhotoViewGallery.

In other words, if we have a list of images to view, we can use the PhotoView above, if it is a list of pictures, then we need to use PhotoViewGallery.

Usually we use the PhotoViewGallery.builder () method, so let's take a look at the constructor:

PhotoViewGallery.builder (scrollPhysics: BouncingScrollPhysics (), / / interaction to the boundary default Android effect scrollDirection: Axis.horizontal,// sliding direction default horizontal reverse: false,// whether to reverse the sliding reading order direction default false,true horizontal Slide the picture from right to left builder: _ buildItem,// picture constructor itemCount: widget.bigImageList.length, / / number of pictures loadingBuilder: widget.loadingBuilder? / / the components displayed during the picture loading process can display the loading progress (context, e) {return MyImage (image: MyImage.defImg) }, backgroundDecoration: widget.backgroundDecoration?? / background style Custom BoxDecoration (color: Colors.black87), scaleStateChangedCallback: (photoViewScaleState) {/ / callback when the user double-clicks the picture to zoom in and out}, whether enableRotation:false,// supports gesture rotation of the picture customSize: MediaQuery.of (context). Size, / / defines the size of the default zoom base of the picture, default MediaQuery.of (context). Size allowImplicitScrolling: true / / whether to allow implicit scrolling to provide a field for the visually impaired default false pageController: widget.pageController, / / switch picture controller onPageChanged: (index) {/ / picture switch callback setState (() {this.index = index + 1) });},)

We can see that the builder method is used to load images, so let's take a look at the builderItem method:

We can see that what is returned is the PhotoViewGalleryPageOptions object, which is the concrete class that loads the image. Here are some common construction methods. This class also supports gesture-related callbacks, which can be studied by yourself if you are interested. There won't be too much introduction here.

PhotoViewGalleryPageOptions _ buildItem (BuildContext context, int index) {final BigImageBean item = widget.bigImageList [index] Return PhotoViewGalleryPageOptions (/ / Image Loader supports local and network imageProvider: NetworkImage (item.imageUrl? "), / / initialization size all display initialScale: PhotoViewComputedScale.contained, / / minimum display zoom minScale: PhotoViewComputedScale.contained * 0.5, / / maximum display zoom maximum maxScale: PhotoViewComputedScale.covered * 4 / / hero animate heroAttributes: PhotoViewHeroAttributes (tag: item.imageUrl? ""),) }

At this point, we have completed the preview of the picture of the large image operation, is not very simple. After you can view it, then we need to save this picture to an album, which is even easier. Let's take a look at how the image_gallery_saver plugin saves the picture.

One is to save through the byte array, the other is to save the file, it is very simple, just convert the network picture into bytecode and then call save, of course, here we need to verify the file storage permissions, permissions verification plug-in: permission_handler, here we convert the network picture into bytes through the dio network library, and the network encapsulation library can see another article: the second package of dio.

Future imageToBytes (String imageUrl) async {var response = await _ dio?.get (imageUrl, options: Options (responseType: ResponseType.bytes); return Uint8List.fromList (response?.data);}

Finally, let's show an effect picture:

We found that the picture just now had been saved by us in the album.

The above is about "Android how to achieve picture preview and save function" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report