是否可以将Http.POST与Universal Image Loader一起使用

时间:2014-03-26 02:48:39

标签: android universal-image-loader

我正在使用Universal Image loader尝试使用http POST而非常规GET加载图片。 POST请求将包含BODY(键值对)中的数据。

有没有办法使用Universal Image Loader?

到目前为止,我已尝试为配置扩展BaseImageDownloader,但我认为我错过了其他内容:

public class SmartImageDownloader extends BaseImageDownloader {

/**
 * Constructor
 * @param context
 */
public SmartImageDownloader(Context context) {
    super(context);
}

@Override
public InputStream getStream(String imageUri, Object extra) throws IOException {
    switch (Scheme.ofUri(imageUri)) {
        case HTTP:
        case HTTPS:
            KLog.e(TAG, "getStream: " + extra.toString(), KLog.Severity.WTF);
            HttpURLConnection conn = createConnection(imageUri, extra);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

            writer.write(getQuery((List<NameValuePair>)extra));
            writer.flush();
            writer.close();
            os.close();


            return new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE);
        case FILE:
            return getStreamFromFile(imageUri, extra);
        case CONTENT:
            return getStreamFromContent(imageUri, extra);
        case ASSETS:
            return getStreamFromAssets(imageUri, extra);
        case DRAWABLE:
            return getStreamFromDrawable(imageUri, extra);
        case UNKNOWN:
        default:
            return getStreamFromOtherSource(imageUri, extra);
    }
}

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}
}

我尝试通过调用以下内容来加载适配器中的图像:

ImageLoader.getInstance().displayImage(IMAGEURL,
                                             IMAGEVIEW,
                                            DisplayOptionsUtil.DefaultDisplayImageOptions(ImageUtil.GenerateRequestParamsForImage(cnf, ImageSize.LARGE)));

其中:

public static final DisplayImageOptions DefaultDisplayImageOptions(List<NameValuePair> params){
    return new DisplayImageOptions.Builder()
            .imageDownloader(new SmartImageDownloader(this))
            .resetViewBeforeLoading(true)
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
            .bitmapConfig(Bitmap.Config.ARGB_8888)
            .extraForDownloader(params)
            .build();
}

我想要做的是,为了加载图像视图的图像,我构建了一个DisplayImageOptions,其中post params为extras。目前它不断回来说java.io.FileNotFoundException。任何人都可以给我一个指针?谢谢!

0 个答案:

没有答案