Android fill ImageView from URL

試著忘記壹切 提交于 2019-12-11 05:28:18

问题


Hi i'm trying to add an image to an ImageView from a URL i have tried loading it as a bitmap but nothing is showing. so does anyone know what the best method to do this is or what i'm doing wrong?

heres my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //Check Preferences which sets UI
    setContentView(R.layout.singlenews);
      TextView headerText = (TextView) findViewById(R.id.header_text);
       headerText.setText("Latest News");


     PostTask posttask;
       posttask = new PostTask();
       posttask.execute();
}

public void loadNews(){

    newsStr = getIntent().getStringExtra("singleNews");




    try {
        JSONObject obj = new JSONObject(newsStr);
        content = obj.getString("content");
          title = obj.getString("title");
          fullName = obj.getString("fullname");
          created = obj.getString("created");
        NewsImageURL = obj.getString("image_primary");
        tagline = obj.getString("tagline");

        meta = "posted by: " + fullName + " " + created;


        URL aURL = new URL("NewsImageURL");
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        /* Buffered is always good for a performance plus. */
        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
         bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        /* Apply the Bitmap to the ImageView that will be returned. */






        Log.v("lc", "content=" + content);
        Log.v("lc", "title=" + title);
        Log.v("lc", "fullname=" + fullName);
        Log.v("lc", "created=" + created);
        Log.v("lc", "NewsImage=" + NewsImageURL);
        Log.v("lc", "Meta=" + meta);
        Log.v("lc", "tagline=" + tagline);







} catch 
 (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}




public class PostTask extends AsyncTask<Void, String, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        boolean result = false;
        loadNews();
        publishProgress("progress");
        return result;
    }

    protected void onProgressUpdate(String... progress) {
        StringBuilder str = new StringBuilder();
            for (int i = 1; i < progress.length; i++) {
                str.append(progress[i] + " ");

            }
    }

        @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.v("BGThread", "begin fillin data");

        fillData();

        }
}

public void fillData(){

    NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.newsdetailact,
            null);

  TextView Title = (TextView) NewsView.findViewById(R.id.NewsTitle);
  Title.setText(title);

  TextView Tagline = (TextView) NewsView.findViewById(R.id.subtitle);
  Tagline.setText(tagline);

  TextView MetaData = (TextView) NewsView.findViewById(R.id.meta);
  MetaData.setText(meta);




     ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
     NewsImage.setImageBitmap(bm);

  TextView MainContent = (TextView) NewsView.findViewById(R.id.maintext);
  MainContent.setText(content);





  Log.v("BGThread", "Filled results");

adapter = new MergeAdapter();

adapter.addView(NewsView);


setListAdapter(adapter);


}



}

回答1:


Please use this code spinet convert url images to bitmap and show it in image view .

URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

T think it help you.

Thanks




回答2:


I use Prime for all of my image loading, is handles situations like this with ease.




回答3:


          String url1 = "Your URL....";

                    URL ulrn = new URL(url1);
                    HttpURLConnection con = (HttpURLConnection) ulrn
                            .openConnection();
                    InputStream is = con.getInputStream();
                    bmp1 = BitmapFactory.decodeStream(is);

                    int widthPx = 150;  //you can set width
                    int heightPx = 150; //you can set height
                    bmp1=Bitmap.createScaledBitmap(bmp1, widthPx, heightPx, true); 

         Imgview1.setImageBitmap(bmp1);



回答4:


In situation like this I'd try to write the InputStream into a file first then load from file. That usually works. Another reason to justify writing into file is because I prefer to lazy load the image from internet as it could take much longer than expected. In this case, I suspect that the InputStream was closed before it has a chance to finish the process.

Yours code and mine structured pretty much the same without the copying into file part.

If you wish to investigate, I'd recommend you doing this:

  1. Check that the image is actually exists.
  2. Check and compare size of the Stream and Bitmap.
  3. Check the InpuStream to see if it was closed unexpectedly.


来源:https://stackoverflow.com/questions/11053085/android-fill-imageview-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!