android保存png及gif到本地指定路径

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();
        }
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值