简体   繁体   English

在Android中将图像URL转换为可绘制资源ID

[英]Convert image URL to drawable resource id in android

I'm using the KenBurnsView library with this code: 我使用带有以下代码的KenBurnsView库:

mHeaderPicture.setResourceIds(R.drawable.picture0, R.drawable.picture1);

As you can see it takes drawable resource ids. 如您所见,它带有可绘制的资源ID。

What I want to do is covert all photos URLS to drawable resource ids. 我要做的是将所有照片URLS转换为可绘制资源ID。 Photo urls like this: 照片网址如下:

http://example.com/image.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg

I tried this code here and it didn't work: 我在这里尝试了这段代码,但没有成功:

Bitmap drawable_from_url(String url) throws java.net.MalformedURLException, java.io.IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();
    connection.setRequestProperty("User-agent","Mozilla/4.0");

    connection.connect();
    InputStream input = connection.getInputStream();

    x = BitmapFactory.decodeStream(input);
    return x;
}

I have read a LOT of questions and answers all over the web and I didn't find a good tutorial or solution. 我已经在网络上阅读了很多问题和答案,但找不到很好的教程或解决方案。

Try to drop this library into your project. 尝试将此库放入您的项目中。 Its a useful library. 它是一个有用的库。 http://square.github.io/picasso/ http://square.github.io/picasso/

Sample Usage: 样品用法:

Picasso.with(context).load("http://example.com/image.jpg").into(imageView);

How to get the drawable from imageView: 如何从imageView获取可绘制对象:

Drawable myDrawable = imageView.getDrawable();

You can't convert a Bitmap to a resource id. 您不能将位图转换为资源ID。 Resource ids are only for resources in your APK. 资源ID仅适用于APK中的资源。 You'll have to edit or extend the KenBurnsView class to accept Bitmap objects eg by adding a function like this: 您必须编辑或扩展KenBurnsView类以接受Bitmap对象,例如,通过添加如下函数:

public void setBitmaps(Bitmap... bitmaps) {
    for (int i = 0; i < mImageViews.length; i++) {
        mImageViews[i].setImageBitmap(bitmaps[i]);
    }
}

Then you can pass the Bitmaps that you load with drawable_from_url() 然后,您可以使用drawable_from_url()传递加载的位图

You just forgot to add setDoInput to connection 您只是忘记将setDoInput添加到连接中

public Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Add asynchronous task ... 添加异步任务...

new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute(url);
}
public void onClick(View v) {
    startActivity(new Intent(this, IndexActivity.class));
    finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView mHeaderPicture;
public DownloadImageTask(ImageView mHeaderPicture){
        this.mHeaderPicture= mHeaderPicture;
    }  protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        mHeaderPicture.setImageBitmap(result);
    }
}`

Make sure you have the following permissions set in your AndroidManifest.xml to access the internet. 确保您在AndroidManifest.xml中设置了以下访问互联网的权限。

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

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

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