2017-05-02 35 views
0

我正在尝试创建壁纸应用程序以显示基于可绘制文件夹的一系列图像。我如何从URL获取这些图像,即通过url来获取图像?如何通过URL显示图像的范围,而不是可绘制的

public class ImageAdapter extends BaseAdapter { 

    private Context mContext; 

    public Integer[] mThumbIds = { 
     R.drawable.wallpaper_1, 
     R.drawable.wallpaper_2, 
     R.drawable.wallpaper_3, 
     R.drawable.wallpaper_4, 
     R.drawable.wallpaper_5, 
     R.drawable.wallpaper_6, 
     R.drawable.wallpaper_7, 
     R.drawable.wallpaper_8, 
     R.drawable.wallpaper_9, 
     R.drawable.wallpaper_10 
    }; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(mContext); 
     imageView.setImageResource(mThumbIds[position]); 
     imageView.setScaleType(imageView.getScaleType().CENTER_CROP); 
     imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 400)); 
     return imageView; 
    } 

} 
+0

你试图从可扩展的文件夹或从远程服务器加载图像? – Radhey

回答

0

比方说你的图片网址类似http://myweb.com/images/1.jpg和它的票样2.jpg3.jpg和...

现在你只需要在你的getView方法使用该行:

Picasso.with(getContext()).load("http://myweb.com/images/"+(position+1)+".jpg").fit().into(imageView); 

顺便说一句,你可以在你的gradle中添加毕加索库: compile 'com.squareup.picasso:picasso:2.5.2'

UPDATE:如果你有你的URL列表中,您必须更改Picasso一行:

Picasso.with(getContext()).load(listOfUrls.get(position)).fit().into(imageView); 
0
public String[] mThumbIds = {url1,url2,....,url10}; 

在getView方法中添加以下代码

URL url = new URL(mThumbIds [position]); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
imageView.setImageBitmap(bmp); 

编辑:

Load image from url 在链接看到给凯尔克莱格 答案如何从URL加载图像的ImageView在一个单独的线程。

+0

'网址URL =新的URL(mThumbIds [位置]);' 给我一个'未处理的异常java.net.malformedurlexception' 和 'BMP位图= BitmapFactory.decodeStream(url.openConnection()的getInputStream()) ;' 也给我:'未处理的异常java.io.ioexception' – MrMR

相关问题