使用URL将图像加载到TextView

时间:2018-06-28 06:29:21

标签: android image url download

我正在尝试使用url下载图像并将其设置为TextView背景。

    Bitmap bmImg;
URL myfileurl =null;
try{
    myfileurl= new URL("https://www.vegetables.co.nz/assets/vegetables/_resampled/FillWyI0MDAiLCIzMDAiXQ/carrots.png");
}
catch (MalformedURLException e){
    e.printStackTrace();
}

try{
    HttpURLConnection conn= (HttpURLConnection)myfileurl.openConnection();
    conn.setDoInput(true);

    conn.connect();
    InputStream is = conn.getInputStream();
    BitmapFactory.Options options = new BitmapFactory.Options();

    bmImg = BitmapFactory.decodeStream(is,null,options);
    Drawable dr = new BitmapDrawable(bmImg);
    tv.setBackgroundDrawable(dr);
    tv.setImageBitmap(bmImg);
}
catch(IOException e){
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(PhotoRating.this, "Connection Problem. Try Again.", Toast.LENGTH_SHORT).show();
}

我收到错误->

  

AndroidRuntime:致命异常:主要                     流程:com.example.android.architecture.blueprints.tododatabinding.mock,PID:   20288                     android.os.NetworkOnMainThreadException                         在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)                         在java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)                         在java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)                         在java.net.InetAddress.getAllByName(InetAddress.java:752)                         在com.android.okhttp.internal.Network $ 1.resolveInetAddresses(Network.java:29)                         在com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:209)                         在com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:163)                         在com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:105)                         在com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:489)                         在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)                         在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)                         在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)                         在com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:130)                         在com.example.android.architecture.blueprints.todoapp.productlist.ProductlistFragment $ 2.getView(ProductlistFragment.java:200)                         在android.widget.AbsListView.obtainView(AbsListView.java:3170)                         在android.widget.GridView.onMeasure(GridView.java:1104)

我在INTERNET Permission文件中提供了Manifest.xml。我不知道错误是什么。

选项2:

我也尝试过Picasso,将依赖项compile com.squareup.picasso:picasso:2.4.0添加到a​​pp / build.gradle

TextView tv = (TextView) findViewById(R.id.textView1);
Picasso.with(this)                        .load("https://www.vegetables.co.nz/assets/vegetables/_resampled/FillWyI0MDAiLCIzMDAiXQ/carrots.png").into(tv);

它显示

cannot find symbol variable Picasso

0 个答案:

没有答案