在课堂之间丢失数据

时间:2014-08-30 14:45:47

标签: java android

我有基本的下载器类 我下载和解码位图的地方。 但如果我尝试解码这个类,我得到我的bitmap = null。

public void add_item(String _txt, String _url) {
    try {
        Downloader dw = new Downloader(_url, context);
        InputStream s = dw.execute().get();
        Bitmap b = BitmapFactory.decodeStream(s); // <<<<<< bitmap is null
        bmp.add(b);
    }  catch (ExecutionException e) { Toast.makeText(this.context, "Ошибка загрузки картинки", Toast.LENGTH_SHORT).show(); }
       catch (InterruptedException e) { Toast.makeText(this.context, "Ошибка загрузки картинки", Toast.LENGTH_SHORT).show(); }
}

downloader.java

package com.example.john.weather;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Downloader extends AsyncTask<Void, Void, InputStream> {

String url;
Context mContext;

public Downloader(String _url, Context _context){
    this.url = _url;
    this.mContext = _context;
}

@Override
protected InputStream doInBackground(Void... params) {
    InputStream s;
    try{
        s = download(this.url);
        //Bitmap b = BitmapFactory.decodeStream(s); // <<<<<< Bitmap is good
        return s;
    }
    catch (MalformedURLException e) {
        Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
        return null; }
    catch (IOException e){
        Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
        return null;
    }
}

private InputStream download(String url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    return input;
}

}

我有基本的下载器类 我下载和解码位图的地方。 但如果我尝试解码这个类,我得到我的bitmap = null。

2 个答案:

答案 0 :(得分:0)

问题很可能是在Downloader方法的download()类中。在将执行移动到另一个线程(在这种情况下是主线程)之前,您需要加载流的内容。

答案 1 :(得分:0)

是的,只需找到解决方案。 将InputStream移入byte []然后返回。 因此可以完成解码

byte[] data = dw.execute().get();
Bitmap b = BitmapFactory.decodeByteArray(data,0,data.length);

downloader.java

package com.example.john.weather;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Downloader extends AsyncTask<Void, Void, byte[]> {

String url;
Context mContext;

public Downloader(String _url, Context _context){
    this.url = _url;
    this.mContext = _context;
}

@Override
protected byte[] doInBackground(Void... params) {
    InputStream s;
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try{
        s = download(this.url);

        /*s.available() can be used only for small downloaded files
        Returns an estimated number of bytes that can be read or skipped without blocking for more input.
        Note that this method provides such a weak guarantee that it is not very useful in practice....*/
        byte[] data = new byte[s.available()];
        int n;
        while ((n = s.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, n);
        }
        buffer.flush();

        return buffer.toByteArray();
    }
    catch (MalformedURLException e) {
        Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
        return null; }
    catch (IOException e){
        Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
        return null;
    }
}

@Override
protected void onPostExecute(byte[] result) {

}

private InputStream download(String url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    return input;
}

}

UPD。更稳定的转换为byte []

@Override
protected byte[] doInBackground(Void... params) {
    InputStream input;
    try{
        input = download(this.url);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = input.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toByteArray();
    }
    catch (MalformedURLException e) { Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show(); return null; }
    catch (IOException e){ Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show(); return null; }
}