简体   繁体   English

图片未在Android App上显示

[英]Image not showing on Android App

I am trying to get a jpeg from a url and place it in an image view in my android app. 我正在尝试从网址获取jpeg,并将其放置在我的android应用中的图片视图中。 I am using a method that turns an image from the url into a drawable. 我正在使用一种将URL中的图像转换为可绘制图像的方法。 However, the image is not showing up, the image view is just blank 但是,图像没有显示,图像视图只是空白

Here is the link to the jpeg: http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg 这是jpeg的链接: http : //upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg

Method: 方法:

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

Main function: 主功能:

public class MainActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    ImageView profilePhoto = (ImageView)findViewById(R.id.personImage);

    String url = "http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg";

    profilePhoto.setImageDrawable(LoadImageFromWebOperations(url));
}

I would suggest using Picasso instead. 我建议改用毕加索

With Picasso, it's only one line of code to load an image into an ImageView from a URL. 使用毕加索,仅需一行代码即可将图像从URL加载到ImageView中。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

I suggest you load it in a Bitmap: 我建议您将其加载到位图中:

URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg");
Bitmap bit = BitmapFactory.decodeStream(url.openConnection().getInputStream());
profilePhoto.setImageBitmap(bit);

I would use an AsyncTask, since you have to connect to the internet to get the image: 我将使用AsyncTask,因为您必须连接到互联网才能获取图像:

So put this inside your activity : 因此,将其放入您的活动中:

ImageView profilePhoto = (ImageView)findViewById(R.id.personImage);
 new LoadPictureAsyncTask(MainActivity.this, profilePhoto).execute();

And add this subclass: 并添加以下子类:

public class LoadPictureAsyncTask extends AsyncTask<Void, Void, Bitmap> {       
    private Context context;        
    private ImageView myProfPic;        

    public LoadPictureAsyncTask(Context mycontext,ImageView ProfPic){
        this.context = mycontext;           
        this.myProfPic = ProfPic;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPreExecute()
    {       }

    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bitmap;
        try
        {

        URL imageURL = new URL("http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg");
            bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
        }catch(Exception ex)
        {
             bitmap = null;
        }                               

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result)
    {                   
        if(result!=null)
            {                               
                myProfPic.setImageBitmap(result);               
            }           
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM