android图片下载到本地
·
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
};
@Override
protected 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 permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.download) {
new Thread(new MyRunnable()).start();
}
}
class MyRunnable implements Runnable {
@Override
public 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() {
@Override
public 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
更多推荐


所有评论(0)