android图片下载到本地

news/2024/11/17 6:32:47/

1.联网权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.INTERNET" />

2.主界面网络下载

package com.example.glidedownload;import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;import java.io.File;
import java.util.Calendar;
import java.util.concurrent.ExecutionException;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Context mContext;private Button download;private ImageView img;private File target = null;// public static String PICTURE_s04 = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=339074543,1379749639&fm=26&gp=0.jpg";//   public static String PICTURE_04 = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1774420118,989846740&fm=26&gp=0.jpg";public static String PIsCTURE_01 = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1652123795,1945063222&fm=26&gp=0.jpg";public static String PIsssCTURE_01 = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3574546774,2747680979&fm=11&gp=0.jpg";public static String PICTURE_01 = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2807156066,2009521216&fm=11&gp=0.jpg";private String path;/*    private static final String URL ="https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture";*/public static String FILENAME = Environment.getExternalStorageDirectory() + "/Download/picture.jpg";private static final String TAG = "MainActivity";private static final int REQUEST_EXTERNAL_STORAGE = 1;private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = this;download = findViewById(R.id.download);img = findViewById(R.id.img);target = new File(FILENAME);download.setOnClickListener(this);verifyStoragePermissions(this);if (target.exists()) {Glide.with(mContext).load(target).into(img);}}/*** 在对sd卡进行读写操作之前调用这个方法* Checks if the app has permission to write to device storage* If the app does not has permission then the user will be prompted to grant permissions*/public static void verifyStoragePermissions(Activity activity) {// Check if we have write permissionint permission = ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE);if (permission != PackageManager.PERMISSION_GRANTED) {// We don't have permission so prompt the userActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}}@Overridepublic void onClick(View v) {if (v.getId() == R.id.download) {new Thread(new MyRunnable()).start();}}class MyRunnable implements Runnable {@Overridepublic void run() {downloadImage();getImageView();}}private void getImageView() {//  val saveAs = "Your_Created_Image_File_Path"Uri contentUri = Uri.fromFile(new File(path));Log.e(TAG, "onGetMessage_like: " + path);Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);sendBroadcast(mediaScanIntent);}private String downloadImage() {Glide.get(getApplicationContext()).clearDiskCache();File file = null;try {file = Glide.with(mContext).load(PICTURE_01).downloadOnly(96, 96).get();} catch (Exception e) {e.printStackTrace();}path = FileUtil.copy(file, target);//    MediaStore.Images.Media.insertImage(getContentResolver(),.getPath(),"cameraPhoto", "Camera Pic Sample App Took");runOnUiThread(new Runnable() {@Overridepublic void run() {Glide.with(mContext).load(target).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(img);}});return path;}}

3.文件类

package com.example.glidedownload;import android.os.Environment;
import android.util.Log;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*** Created by zhc on 2019/12/19.*/
public class FileUtil {private static final String TAG = "FileUtil";/*** 复制文件* @param filename 文件名* @param bytes 数据*/public static void copy(String filename, byte[] bytes) {try {//如果手机已插入sd卡,且app具有读写sd卡的权限if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {FileOutputStream output = null;output = new FileOutputStream(filename);output.write(bytes);Log.i(TAG,"copy success, " + filename);output.close();} else {Log.i(TAG, "copy fail, " + filename);}} catch (IOException e) {e.printStackTrace();}}/*** 复制文件** @param source 输入文件* @param target 输出文件*/public static String copy(File source, File target) {FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream(source);fileOutputStream = new FileOutputStream(target);byte[] buffer = new byte[1024];while (fileInputStream.read(buffer) > 0) {fileOutputStream.write(buffer);}} catch (Exception e) {e.printStackTrace();} finally {try {if (fileInputStream != null) {fileInputStream.close();}if (fileOutputStream != null) {fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}return target.getPath();}}

4.打开相册,选择全部图片,可以看到刚刚下载的图片

end


http://www.ppmy.cn/news/861213.html

相关文章

uniapp 下载图片并保存到手机的相册中

使用unaipp开发的微信小程序中&#xff0c;下载图片并保存到手机的相册中。 创建公共方法文件 common.js&#xff0c;相关api可以自行查阅微信开发文档了解&#xff0c;参照代码如下&#xff1a; let baseUrl https://tese.com; const getUpLoadFile async function (fileId…

手机端长按图片下载

手机端长按图片下载 一版html长按事件下载js效果图 二版(贴合App)htmljs效果图 一版 html <div id"target"><img src"./timg.jfif" id"img1" class"img1"></img> </div> $(function () {// 函数名longpress…

Android下载网络图片并保存到相册

下载类&#xff0c;可以url下载到相册&#xff0c;记得在清单加权限&#xff0c;6.0代码动态加权限判断&#xff0c;下载图片要在子线程中下载&#xff0c;下载完后广播更新相册 在清单文件里面添加权限&#xff1a; <!--网络--><uses-permission android:name"…

html5 plus 图片下载保存到手机相册

html5plus 接口文档&#xff1a;http://www.html5plus.org/doc/zh_cn/webview.html vue 集成html5plus方法&#xff1a;https://blog.csdn.net/weixin_38641550/article/details/85235297 vue集成plus demo : https://github.com/Shaxin742/vue-webapp-demo 这个是基于html…

网上下载图片并保存到手机里面

MainActivity 里面用的是异步任务下载的图片 package com.jj.rikao_15;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.A…

数学建模-拟合算法

这里的线性函数指的是参数为线性&#xff0c;而不是变量为线性。 yabx^2是线性的 用的比较多的是多项式拟合和自己定义的 拓展资料&#xff1a;工具箱曲线拟合类型评价解释 文件-导出代码 自动生成的代码修改图名和标签 如果不收敛&#xff0c;自己要修改初始值&#xf…

综合小实验

第一步&#xff1a;计划IP R1的环回&#xff1a;192.168.1.0/28 R2的环回&#xff1a;192.168.1.16/28 R123的O/O/0接口&#xff1a;192.168.1.32/28 R3-4&#xff1a;192.168.1.128/30 Vlan2&#xff1a;192.168.1.48/28 vlan3&#xff1a;192.168.1.64/28 192.168.1.0/24 0区…

Active Directory域服务

1.什么是域服务域内的directory database(目录数据库)被用来存储用户账号&#xff0c;计算机账户&#xff0c;打印机与共享文件夹等对象&#xff0c;而提供目录服务的组件就是Active Directory域服务(Active Directory Domin Service,AD DS)它负责目录数据库的存储、新建、删除…