在展示图片的时候,我们不仅仅需要把图片展示出来,往往还需要把图片下载到本地,这里举例取出Glide的GIF图片放到指定的文件目录,之前写过一篇如何加载GIF:
取出Glide中文件的地址:
/**
* Glide 获得图片缓存路径
*/
private String getImagePath(String imgUrl) {
String path = null;
FutureTarget<File> future = Glide.with(this)
.load(imgUrl)
.downloadOnly(500,500);
try {
File cacheFile = future.get();
path = cacheFile.getAbsolutePath();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return path;
}
如果你直接在主线程取缓存路径的话会报错:
java.lang.IllegalArgumentException: YOu must call this method on a background thread
说明这个必须在子线程去操作。
权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
依赖:Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
Gif 图片地址:
全部代码
1,布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.dhl.myapplication.GifDownLoad">
<ImageView
android:id="@+id/image"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"/>
<Button
android:id="@+id/downLoadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载图片"
android:layout_below="@id/image"
/>
</RelativeLayout>
2,code:
public class GifDownLoad extends AppCompatActivity {
private ImageView image ;
private Button downLoadBtn ;
private static String url = "http://qq.yh31.com/tp/zjbq/201612231514480890.gif" ;
/**
* 对保存的图片命名
*/
private String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"201612231514480890.gif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gif_down_load);
image = (ImageView)findViewById(R.id.image);
downLoadBtn = (Button)findViewById(R.id.downLoadBtn);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},0);
}
Glide.with(this).load(url).into(image);
downLoadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//String path = getImagePath(url);
new Thread(new Runnable() {
@Override
public void run() {
String path = getImagePath(url);
/**
* 拷贝到指定路径
*/
copyFile(path, imagePath);
Intent intentBroadcast = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(imagePath);
intentBroadcast.setData(Uri.fromFile(file));
sendBroadcast(intentBroadcast);
}
}).start();
}
});
}
/**
* Glide 获得图片缓存路径
*/
private String getImagePath(String imgUrl) {
String path = null;
FutureTarget<File> future = Glide.with(this)
.load(imgUrl)
.downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL);
try {
File cacheFile = future.get();
path = cacheFile.getAbsolutePath();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return path;
}
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
获取Gldie 展示图片到指定文件目录,就完美结束了。