0

我正在尝试在 ListView 上显示图像。代码片段:

private String[] movieURL = {"..","..",".."};

try {
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent());
            holder.image.setImageBitmap(bitmap);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

这是 ViewHolder 类:

private static class ViewHolder {
    public TextView text1;
    public TextView text2;
    public ImageView image;
  }

但它什么也没显示。我认为代码没问题。和堆栈跟踪:

12-14 07:50:54.975: D/dalvikvm(1865): GC_CONCURRENT freed 122K, 8% free 3082K/3324K, paused 4ms+28ms, total 123ms
12-14 07:50:55.175: D/dalvikvm(1865): GC_FOR_ALLOC freed 16K, 8% free 3178K/3424K, paused 40ms, total 51ms
12-14 07:50:55.195: I/dalvikvm-heap(1865): Grow heap (frag case) to 4.291MB for 1127536-byte allocation
12-14 07:50:55.286: D/dalvikvm(1865): GC_FOR_ALLOC freed <1K, 6% free 4278K/4528K, paused 84ms, total 86ms
12-14 07:50:55.395: D/dalvikvm(1865): GC_CONCURRENT freed <1K, 6% free 4279K/4528K, paused 10ms+20ms, total 118ms
12-14 07:50:55.547: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.625: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.685: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.715: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:55.815: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0
12-14 07:50:57.125: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:57.875: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:58.736: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:59.386: D/skia(1865): --- SkImageDecoder::Factory returned null
12-14 07:50:59.945: D/skia(1865): --- SkImageDecoder::Factory returned null
4

3 回答 3

0

试试这样

holder.image.setImageBitmap(BitmapFactory.decodeFile(movieurl
                .get(position)));
于 2012-12-14T08:03:59.150 回答
0

你确定Bitmap-object 不为空吗?做一些记录..

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent());

if(bitmap == null) {
    Log.i("Aha!", "The Bitmap was null all this time :)");
} 

holder.image.setImageBitmap(bitmap);

如果您Bitmap为 null,请检查 URL 是否实际指向您尝试获取的资源。

另外,记得包括

<uses-permission android:name="android.permission.INTERNET"/> 

在你的AndroidManifest.xml. 不这样做会给你一个Bitmap没有任何错误的空值。

于 2012-12-14T08:31:02.070 回答
0

尝试下面的代码使用 AsyncTask 从 Web 下载图像并显示在 imageview 中,它将解决您的问题。

public class MainActivity extends Activity {

    ImageView mImgView1;
    static Bitmap bm;
    ProgressDialog pd;
    String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
    BitmapFactory.Options bmOptions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImgView1 = (ImageView) findViewById(R.id.mImgView1);
        pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
                "Carregando...");
        new ImageDownload().execute("");
    }

    public class ImageDownload extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            loadBitmap(imageUrl, bmOptions);
            return imageUrl;
        }

        protected void onPostExecute(String imageUrl) {
            pd.dismiss();
            if (!imageUrl.equals("")) {
                mImgView1.setImageBitmap(bm);
            } else {
                Toast.makeText(MainActivity.this,
                        "Não foi possível obter resultados", Toast.LENGTH_LONG)
                        .show();
            }
        }

    }

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bm = BitmapFactory.decodeStream(in, null, options);
            in.close();
        } catch (IOException e1) {
        }
        return bm;
    }

    private static InputStream OpenHttpConnection(String strURL)
            throws IOException {
        InputStream inputStream = null;
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }
}
于 2012-12-14T09:15:16.187 回答