Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上显示URL中的图像_Android_Image_Url_Https - Fatal编程技术网

如何在Android上显示URL中的图像

如何在Android上显示URL中的图像,android,image,url,https,Android,Image,Url,Https,我想在屏幕上显示图像。图像应该来自URL,而不是可绘制的 代码如下: <ImageView android:id="@+id/ImageView01" android:src = "http://l.yimg.com/a/i/us/we/52/21.gif" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 但它在编译时会出错 如何在An

我想在屏幕上显示图像。图像应该来自URL,而不是可绘制的

代码如下:

<ImageView android:id="@+id/ImageView01" android:src = "http://l.yimg.com/a/i/us/we/52/21.gif"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

但它在编译时会出错

如何在Android中显示URL中的图像?

举个简单的例子,

您必须使用httpClient并下载映像(如果需要,请缓存映像)

为在listview中显示图像提供的解决方案,本质上是用于显示的相同代码(检查从url设置imageview的代码)


您可以直接从web上显示图像,而无需下载。请检查以下功能。它会将来自web的图像显示到您的图像视图中

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;
    }
}
然后使用活动中的代码将image设置为imageview

InputStream URLcontent = (InputStream) new URL(url).getContent();
Drawable image = Drawable.createFromStream(URLcontent, "your source link");

这对我很有效

我从URL中重试了一个图像,并使用以下代码存储在SD卡上:

public String Downloadfromurl(String Url)
{

 String filepath=null;

 try {

  URL url = new URL(Url);

  //create the new connection

  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

  //set up some things on the connection
  urlConnection.setRequestMethod("GET");

  urlConnection.setDoOutput(true); 

   //and connect!

  urlConnection.connect();

  //set the path where we want to save the file
  //in this case, going to save it on the root directory of the
  //sd card.

  folder = new File(Environment.getExternalStorageDirectory().toString()+"/img");

  folder.mkdirs();

  //create a new file, specifying the path, and the filename
  //which we want to save the file as.

  String filename= "page"+no+".PNG";   

  file = new File(folder,filename);

  if(file.createNewFile())

  {

   file.createNewFile();

  }

  //this will be used to write the downloaded data into the file we created
  FileOutputStream fileOutput = new FileOutputStream(file);

  //this will be used in reading the data from the internet
  InputStream inputStream = urlConnection.getInputStream();

  //this is the total size of the file
  int totalSize = urlConnection.getContentLength();
  //variable to store total downloaded bytes
  int downloadedSize = 0;

  //create a buffer...
  byte[] buffer = new byte[1024];
  int bufferLength = 0; //used to store a temporary size of the buffer

  //now, read through the input buffer and write the contents to the file
  while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
   //add the data in the buffer to the file in the file output stream (the file on the sd card
   fileOutput.write(buffer, 0, bufferLength);
   //add up the size so we know how much is downloaded
   downloadedSize += bufferLength;
   //this is where you would do something to report the prgress, like this maybe
   Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
  }
  //close the output stream when done
  fileOutput.close();
  if(downloadedSize==totalSize)  
      filepath=file.getPath();

 //catch some possible errors...
 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (IOException e) {
  filepath=null;
  e.printStackTrace();
 }
 Log.i("filepath:"," "+filepath) ;


 return filepath;

}

你可以试试这个,我在另一个问题中找到了


使用http处理的
ASyncTask
编写代码

Bitmap b;
ImageView img;
......
try
    {
        URL url = new URL("http://10.119.120.10:80/img.jpg");
        InputStream is = new BufferedInputStream(url.openStream());
        b = BitmapFactory.decodeStream(is);
    } catch(Exception e){}
......
img.setImageBitmap(b);

我也有同样的问题。我测试了这段代码,效果很好。此代码从URL获取图像并放入“bmpImage”


我尝试了这段代码,直接从url获取图像

      private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;
      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }

      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) {
          bmImage.setImageBitmap(result);
      }
    }
私有类下载ImageTask扩展异步任务{
图像视图bmImage;
公共下载ImageTask(ImageView bmImage){
this.bmImage=bmImage;
}
受保护位图doInBackground(字符串…URL){
字符串urldisplay=url[0];
位图mIcon11=null;
试一试{
InputStream in=newjava.net.URL(urldisplay.openStream();
mIcon11=BitmapFactory.decodeStream(in);
}捕获(例外e){
Log.e(“Error”,e.getMessage());
e、 printStackTrace();
}
返回mIcon11;
}
受保护的void onPostExecute(位图结果){
bmImage.setImageBitmap(结果);
}
}
在onCreate()方法中使用

新下载的ImageTask((ImageView)findViewById(R.id.image))
.执行(“”)

你可以试试毕加索,它真的很好而且简单。 不要忘记在清单中添加权限

Picasso.with(context)
                     .load("http://ImageURL")
                     .resize(width,height)
                     .into(imageView );
您还可以在此处查看教程: /
您只需使用Glide API即可。它避免了所有的样板代码,任务可以在两行代码中完成。您可以参考此链接。享受

**如果您想显示SD卡中的图像,请使用此**位图bmp=BitmapFactory.decodeFile(文件夹+”/页面“+否+”.PNG”);设置图像位图(bmp)@希拉格帕特尔:也许,这个链接会回答你:需要互联网许可在这种情况下,“src name”是什么?@Eatlon和其他想知道“src name”在不使用9patch时是一个无用的变量的人。看一看那个讨厌的
android.os.NetworkOnMainThreadException
你导入了必要的文件吗?例如java.io.IOException等等。如果你的设备运行在3.x或4.x上,你需要使用handler或asynctask在UI线程外进行下载操作。因为下载操作在UI线程中被禁止。这个博客可能很有用。基本上“你的源链接”是什么?我有一个谷歌的图像url,我想在上面的代码使用的图像视图加载该图像,但它返回空?如何解决这个问题这将回答以下问题:
url
“您的源链接”
相同?虽然链接可能有用,但最好直接在SO上发布解决方案。步骤1)转到build.gradle并复制粘贴这两个依赖项1)实现'com.github.bumptech.glide:glide:4.9.0'annotationProcessor'com.github.bumptech.glide:compiler:4.9.0',然后在相关布局中创建一个ImageView。3) 把这段代码放到你的Java文件中,但我不得不加上:)这让我发疯了!为什么不为我工作!
      private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;
      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }

      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) {
          bmImage.setImageBitmap(result);
      }
    }
Picasso.with(context)
                     .load("http://ImageURL")
                     .resize(width,height)
                     .into(imageView );