android 下载url图片工具

android端使用fresco下载url图片:

     参考介绍fresco:https://www.cnblogs.com/zhangqie/p/6476892.html

1、准备使用的依赖:

    //图片
    implementation 'com.facebook.fresco:fresco:1.5.0'
    implementation 'com.facebook.fresco:animated-gif:1.5.0'

2、准备需要的权限:

    <!-- 网络权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 允许对sd卡进行读写操作 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 外存储读权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3、初始化使用fresco库:

public class FrescoApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

4、下载图片的工具类:

/**
 * @function: 下载保存http url图片
 * @user 漠天love杨
 */
public class SaveImageUtils {

    private static Uri uri;

    public static Uri saveImage(String url, final String desc, final Context context) {

        ImageRequest imageRequest = ImageRequestBuilder
                .newBuilderWithSource(Uri.parse(url))
                .setProgressiveRenderingEnabled(true)
                .build();

        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>>
                dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);

        dataSource.subscribe(new BaseBitmapDataSubscriber() {
            @Override
            public void onNewResultImpl(Bitmap bitmap) {
                if (bitmap == null) {
                    Toast.makeText(context, "保存图片失败啦,无法下载图片", Toast.LENGTH_SHORT).show();
                }
                File appDir = new File(Environment.getExternalStorageDirectory(), "baiUtil");
                if (!appDir.exists()) {
                    appDir.mkdir();
                }
                String fileName = desc + ".jpg";
                File file = new File(appDir, fileName);
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    assert bitmap != null;
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                uri = Uri.fromFile(file);
                // 通知图库更新
                Intent scannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
                context.sendBroadcast(scannerIntent);
            }
            @Override
            public void onFailureImpl(DataSource dataSource) {
            }
        }, CallerThreadExecutor.getInstance());
        return uri;
    }
}

5、使用工具类下载图片:

class MainActivity : AppCompatActivity() {

    var imageUrl:String = "http://photocdn.sohu.com/20150625/Img415614733.jpg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        saveUrlImage.setOnClickListener {
            SaveImageUtils.saveImage(imageUrl,"fresco image",this)
        }
    }
}

 

发布了106 篇原创文章 · 获赞 19 · 访问量 8万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览