2014-08-30 27 views
1

數據我有基本的下載類 在哪裏下載和解碼位爲好。 但如果我嘗試解碼了這個類的,讓我的位圖= 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; 
} 

} 

我有基本的下載類 在哪裏下載和解碼位爲好。 但如果我嘗試解碼出這個類,我得到我的位圖= null。

+0

如何解碼出這個類? – eldjon 2014-08-30 15:50:02

+0

位圖b = BitmapFactory.decodeStream(s); – user3017844 2014-08-30 15:55:42

回答

0

是的只是找到解決方案。 InputStream被移入字節[]並返回。 所以解碼可以做

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。更穩定的轉換爲字節[]

@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; } 
} 
0

最有可能的問題是download()方法Downloader類中。你需要加載流的內容之前移動執行到另一個線程(這是主線程在這種情況下)

+0

命令dw.execute()會給出你告訴的結果。但是命令dw.execute()。get()等待直到線程完成。 – user3017844 2014-08-30 16:20:22

+0

你可以嘗試解碼postExecute()上的位圖嗎? – eldjon 2014-08-30 16:26:01

+0

剛試過,結果爲NULL。 @Override 保護無效onPostExecute(InputStream結果){ 位圖b = BitmapFactory.decodeStream(結果); b.getWidth(); } – user3017844 2014-08-30 16:34:22