Home » Android » Android: How to fetch or get specific image from any URl or LINK as like facebook?

Android: How to fetch or get specific image from any URl or LINK as like facebook?

Posted by: admin June 15, 2020 Leave a comment

Questions:

i have to use image from any url as like facebook does. But i can’t understood to how it possible in android. and how can i migrate in my application. i show one iOS question and answer that uses facebook graph api for get information from url, see this link for facebook graph API, so please help me for this question solution.

see below image i want like that in android
enter image description here

How to&Answers:

Finally I got answer after long research. That for i have to use JSOUP.jar file and impliment in my project. that can be parse in HTML and we can use to detail from html,
Now i want image, title and description so i will get from HTML.

public class MainActivity extends Activity {

Document document;
String url ;
ProgressDialog mProgressDialog;
TextView t1, t2;
ImageView img;
String title, desc, img_url;
Button btn;
EditText et;
Bitmap bitmap;
String UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView) findViewById(R.id.imgIcon);
    t1 = (TextView) findViewById(R.id.txtTitle);
    t2 = (TextView) findViewById(R.id.txtDesc);
    btn = (Button) findViewById(R.id.button);
    et = (EditText) findViewById(R.id.editText);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            url = et.getText().toString();
            new FetchWebsiteData().execute();
        }
    });
}

private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
    String websiteTitle, websiteDescription, imgurl;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to website
            Document document = Jsoup.connect(url).userAgent(UserAgent).get();
            // Get the html document title
            websiteTitle = document.title();
            Elements description = document.select("meta[name=description]");
            // Locate the content attribute
            websiteDescription = description.attr("content");
            String ogImage = null;
            Elements metaOgImage = document.select("meta[property=og:image]");
            if (metaOgImage != null) {
                imgurl = metaOgImage.first().attr("content");
                System.out.println("src :<<<------>>> " + ogImage);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        t1.setText(websiteTitle + "------" + imgurl);
        t2.setText(websiteDescription);
        Picasso.with(getApplicationContext()).load(imgurl).into(img);
        mProgressDialog.dismiss();
    }

  }
 }

And my XML file for view all data is following:

<?xml version="1.0" encoding="utf-8"?>
<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="com.copy.urlparsing.MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgIcon"
    android:src="@drawable/asf"
    android:layout_alignBottom="@+id/txtDesc"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/txtTitle"
    android:layout_above="@+id/imgIcon"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/txtDesc"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="53dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

Answer:

This you can get by fetching favicon of the website. Google provided one URL, passing domain into query it will fetch image.

The other option is to get it directly from website appending favicon.ico in domain name

Also as you gave the example of Skype, it is fetching preview of url you pass, not any particular image.

(domain/favicon.ico)