图片加载框架四大之一Picasso 加载 Https 图片的那些套路

Picasso

废话少说直接上代码

compile ‘com.squareup.picasso:picasso:2.5.2’
compile ‘com.squareup.okhttp3:okhttp:3.7.0’
package com.example.qundui.learndemo;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.File;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.File;

/**
* Created by qundui on 2017/4/22.
*/

public class PicassoUtils {

public static void loadImageByurl(String url, Context ctx, ImageView imageView) {
    Picasso.with(ctx).load(url).into(imageView);

}
public static void loadImageByRes(int res, Context ctx, ImageView imageView) {
    Picasso.with(ctx).load(res).into(imageView);

}
public static void loadImageByFile(Context ctx, ImageView iv, File file) {
    Picasso
            .with(ctx)
            .load(file)
            .into(iv);
}

public static void loadImageByURI(Context ctx, ImageView iv, int res) {
    Uri uri = resourceIdToUri(ctx, res);

    Picasso
            .with(ctx)
            .load(uri)
            .into(iv);
}

public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";

private static Uri resourceIdToUri(Context context, int resourceId) {
    return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
}

}

package com.example.qundui.learndemo;

import android.net.Uri;

import com.squareup.picasso.Downloader;
import com.squareup.picasso.NetworkPolicy;

import java.io.IOException;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
/**
* Created by qundui on 2017/4/22.
*/
public class ImageDownLoader implements Downloader {
OkHttpClient client = null;
public ImageDownLoader(OkHttpClient client) {
this.client = client;
}
@Override
public Response load(Uri uri, int networkPolicy) throws IOException {

    CacheControl cacheControl = null;
    if (networkPolicy != 0) {
        if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
            cacheControl = CacheControl.FORCE_CACHE;
        } else {
            CacheControl.Builder builder = new CacheControl.Builder();
            if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
                builder.noCache();
            }
            if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
                builder.noStore();
            }
            cacheControl = builder.build();
        }
    }

    Request.Builder builder = new Request.Builder().url(uri.toString());
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }

    okhttp3.Response response = client.newCall(builder.build()).execute();
    int responseCode = response.code();
    if (responseCode >= 300) {
        response.body().close();
        throw new ResponseException(responseCode + " " + response.message(), networkPolicy,
                responseCode);
    }

    boolean fromCache = response.cacheResponse() != null;

    ResponseBody responseBody = response.body();
    return new Response(responseBody.byteStream(), fromCache, responseBody.contentLength());

}

@Override
public void shutdown() {

    Cache cache = client.cache();
    if (cache != null) {
        try {
            cache.close();
        } catch (IOException ignored) {
        }
    }
}

}
package com.example.qundui.learndemo;

import android.app.Application;
import android.net.wifi.WifiConfiguration;

import com.squareup.picasso.Picasso;
import java.util.Collections;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
/**
* Created by qundui on 2017/4/22.
*/

public class Myapp extends Application {
@Override
public void onCreate() {
super.onCreate();
OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
Picasso.setSingletonInstance(new Picasso.Builder(this).
downloader(new ImageDownLoader(client)).loggingEnabled(true)
.build());
}
}
package com.example.qundui.learndemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

/**
* Created by qundui on 2017/4/22.
*/

public class Pictest extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic_test);
iv= (ImageView) findViewById(R.id.iv);
}
public void getPic(View view){
String url=”https://bmc.huochexia.com/qundui-manager/userfiles/9a29fe156da64c66adf862f912f0eeaf/images/banner/2016/12/QQ图片20161219175344.png”;
// Picasso.with(this).setLoggingEnabled(true);
// Picasso.with(this).load(url).into(iv);
PicassoUtils.loadImageByurl(url,this,iv);
}
}

你可能感兴趣的:(android)