android根据URL保存png及gif到本地指定路径
一、保存png到本地
步骤一:根据URL得到bitmap对象
public static Bitmap decodeUriAsBitmapFromNet(String url) {
URL fileUrl = null;
Bitmap bitmap = null;
try {
fileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
步骤二:首先获取bitmap然后将bitmap保存到本地
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = PictureUtil.decodeUriAsBitmapFromNet(url);
PictureUtil.saveImage(bitmap, imageName);
bitmap.recycle();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}).start();
将png保存到本地的方法
public static void saveImage(Bitmap image, String imageName) {
//判断sd卡是否处于挂载状态
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File filesDir = new File(Constant.AdsFolderPath);
File file = new File(filesDir, imageName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Glide 获得图片缓存路径
private static String getImagePath(String imgUrl, Context context) {
String path = null;
FutureTarget<File> future = Glide.with(context)
.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;
}
二、保存gif到本地
步骤一:在子线程获取缓存路径并复制到本地
public static void saveGif(String imgUrl, Context context, String finalImagePath) {
new Thread(new Runnable() {
@Override
public void run() {
//java.lang.IllegalArgumentException: YOu must call this method on a background thread
//必须在子线程中进行
String path = getImagePath(imgUrl, context);
copyFile(path, finalImagePath);
}
}).start();
}
步骤二:获取图片缓存路径
public static void saveGif(String imgUrl, Context context, String finalImagePath) {
new Thread(new Runnable() {
@Override
public void run() {
//java.lang.IllegalArgumentException: YOu must call this method on a background thread
//必须在子线程中进行
String path = getImagePath(imgUrl, context);
copyFile(path, finalImagePath);
}
}).start();
}
步骤三:复制到本地
public static void copyFile(String oldPath, final 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();
}
}