Glide allows you to view images and save images to your mobile phone. glide allows you to view images.

Source: Internet
Author: User

Glide allows you to view images and save images to your mobile phone. glide allows you to view images.

Two Methods: recommended method 1

Method 1 downloadOnly

Create an ImageActivity

Public class ImageActivity extends AppCompatActivity {private static final String TAG = "ImageActivity"; private ImageView iv; Bitmap bitmap; private String mUrl; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_image); iv = (ImageView) findViewById (R. id. iv); // The image url is passed from another Activity. String url = getIntent (). getStr IngExtra (EXTRA_URL); mUrl = url; loadImage (url); // press ImageView to save the image to the mobile phone iv. setOnLongClickListener (new View. onLongClickListener () {@ Override public boolean onLongClick (View v) {download (mUrl) return true ;}) ;}// load and display the image public void loadImage (String url) {Glide. with (this ). load (url ). placeholder (R. drawable. image_loading ). diskCacheStrategy (DiskCacheStrategy. SOURCE ). listener (new RequestListene R <String, GlideDrawable> () {@ Override public boolean onException (Exception e, String model, Target <GlideDrawable> target, boolean isFirstResource) {return false ;} @ Override public boolean onResourceReady (GlideDrawable resource, String model, Target <GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {Log. d (TAG, "onResourceReady: mode:" + model); // if return true; then (Iv) Does not work. You need to manually set the image // iv. setImageDrawable (resource); return false ;}}). into (iv);} // Save the image to the mobile phone public void download (final String url) {new AsyncTask <Void, Integer, File> () {@ Override protected File doInBackground (Void... params) {File file = null; try {FutureTarget <File> future = Glide. with (ImageActivity. this ). load (url ). downloadOnly (Target. SIZE_ORIGINAL, Target. SIZE_ORIGINAL); file = futur E. get (); // first save the image File pictureFolder = Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES ). getAbsoluteFile (); File appDir = new File (pictureFolder, "Beauty"); if (! AppDir. exists () {appDir. mkdirs ();} String fileName = System. currentTimeMillis () + ". jpg "; File destFile = new File (appDir, fileName); FileUtil. copy (file, destFile); // notify the gallery to update sendBroadcast (new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE, Uri. fromFile (new File (destFile. getPath ();} catch (Exception e) {Log. e (TAG, e. getMessage ();} return file;} @ Override protected void onPostExecute (File file) {Toast. makeText (ImageActivity. this, "saved in Pictures/GankBeauty", Toast. LENGTH_SHORT ). show () ;}@ Override protected void onProgressUpdate (Integer... values) {super. onProgressUpdate (values);} cmd.exe cute ();}}

 

 

The above code gets the image url from another activity, and then uses the Glide image loading library to display the image to the ImageView. Long-pressed ImageView can save the image to the Pictures/Beauty directory of the mobile phone.

This is a basic code template, and the actual situation is more complex. For example, Android 6 still has the problem of applying for storage permissions during runtime.

 

Method 2 asBitmap bitmap. compress

There is also a way to save images that many people use, but after comparison I found it is not good, we recommend the above method. The code for another method is as follows:

 

Replace the above loadImage with the following loadBitmap

    public void loadBitmap(String url) {        Glide.with(this).load(url).asBitmap().placeholder(R.drawable.image_loading)                .listener(new RequestListener<String, Bitmap>() {                    @Override                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {                        return false;                    }                    @Override                    public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {                        Log.i(TAG, "onResourceReady: thread is " + Thread.currentThread().getName());                        isReady = true;                        bitmap = resource;                        iv.setImageBitmap(resource);                         return false;                    }                })                .diskCacheStrategy(DiskCacheStrategy.ALL)                .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);    }

 

Replace the above download with the following saveImage

Public void saveImage () {// first save the image File = Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES ). getAbsoluteFile (); File appDir = new File (file, "Beauty"); boolean createed = false; if (! AppDir. exists () {createed = appDir. mkdirs ();} String fileName = System. currentTimeMillis () + ". jpg "; File currentFile = new File (appDir, fileName); FileOutputStream fos = null; try {fos = new FileOutputStream (currentFile); bitmap. compress (Bitmap. compressFormat. JPEG, 100, fos); fos. flush ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} Finally {try {if (fos! = Null) {fos. close () ;}} catch (IOException e) {e. printStackTrace () ;}} // finally notifies the image library to update this. sendBroadcast (new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE, Uri. fromFile (new File (currentFile. getPath ()))));}

In this way, the image is converted into a bitmap, And the bitmap is compressed and saved to the mobile phone.

 

Summary

Download the image from your computer and compare it with the image saved to your mobile phone. It is found that the bitmap method increases the image size.

In the first method, the size of the downloaded image is the same as that of the image downloaded from the computer, and the md5 value is the same.

Therefore, downloadOnly is recommended.

 

 

 

Author: Panda Fang

 

Source: http://www.cnblogs.com/lonkiss/p/7119062.html

 

Original article, reposted, please indicate the author and source, and cannot be used for commercial profit-making activities without permission

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.