Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from url website in imageview in android

I am trying to get image in ImageView from url website but the image not show so, What is the wrong in this code? This is the url of the image.

It is my Main Activity

ImageView i;
private Bitmap bitmap;

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

    i=(ImageView)findViewById(R.id.ImageView1);




    bitmap=GetBitmapfromUrl("http://test-dashboard1.seeloz.com/system/images/products_images/86/5454544114_1401886223?1401886223");
    i.setImageBitmap(bitmap);


}


public Bitmap GetBitmapfromUrl(String scr) {
    try {
        URL url=new URL(scr);
        HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input=connection.getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(input);
        return bmp;



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}}

in the XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="85dp"
        android:layout_marginTop="179dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

my AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.image"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.image.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
like image 231
user3798394 Avatar asked Jul 02 '14 16:07

user3798394


People also ask

How do I display an image from URL in react native?

So here is the solution to insert an image from an external URL in your application: //MyComponent. js import React from 'react'; import { Image } from 'react-native'; const imageUrl = "https://images.unsplash.com/photo-1526045612212-70caf35c14df"; export class MyComponent extends React.

How will you load an image into an ImageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

How do I use image URL in react?

To display an image from a local path in React:Download the image and move it into your src directory. Import the image into your file, e.g. import MyImage from './thumbnail. webp' . Set the src prop of the image element to the imported image, e.g. <img src={MyImage} alt="horse" /> .


1 Answers

Doing network IO in the main thread is evil. Better to avoid.

Also - your url resource access is wrong.

Use something like this instead:

 private Bitmap bmp;

   new AsyncTask<Void, Void, Void>() {                  
        @Override
        protected Void doInBackground(Void... params) {
            try {
                InputStream in = new URL(IMAGE_URL).openStream();
                bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
               // log error
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (bmp != null)
                imageView.setImageBitmap(bmp);
        }

   }.execute();

This is the 'old way' of loading url resources into display. Frankly speaking, I have not written such code in a long time. Volley and Picasso simply do it much better than me, including transparent local cache, multiple loader-threads management and enabling effective resize-before-load policies. All but coffee :)

like image 101
Gilad Haimov Avatar answered Sep 19 '22 20:09

Gilad Haimov