in my app use want to put instagram profile pic in his account
how can I get the Profile Picture of a user from Instagram Programmatically
such as :
Using the Instagram API users endpoint (https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN
) you will receive a response like this one:
{
"data": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
"bio": "This is my bio",
"website": "http://snoopdogg.com",
"counts": {
"media": 1320,
"follows": 420,
"followed_by": 3410
}
}
You can then take the profile_picture and download it using some like Facebook Fresco which will then display the image for you.
Well if you actually want the user to POST a instagram profile picture from your app you can't, instagram hasn't provided the post method for posting photos but yes you can get view and download as per the husky's answer.
Follow these steps to get profile pic in your app
Make a request to below link with username and receive the JSON data.
https://apinsta.herokuapp.com/u/USERNAME
Now parse the id of user from the data.
Now visit this link with the ID
https://i.instagram.com/api/v1/users/ID_HERE/info/
Now parse url key in hd_profile_pic_url_info object.
Load the image in ImageView using Glide.
Happy coding !
Credit to Chrisby for his server.
https://stackoverflow.com/a/49862390/9565955
Actually I'm using a method that doesn't require an Access Token the only thing your need is an username. I'll leave you the code down bellow
Step 1: MainActivity.java
Add this code in to your main activity, create a String function to named getHDProfilePicFromUsername the fuction should look like this:
///This function will return to you the url of the user profile picture
private String getHDProfilePicFromUsername(String username) throws ExecutionException, InterruptedException, JSONException {
JSONObject jObject;
String profileInfo = new getDataFromUrl(MainActivity.this).execute("https://www.instagram.com/"+username+"/?__a=1").get();
jObject= new JSONObject(profileInfo);
jObject = jObject.getJSONObject("graphql");
jObject = jObject.getJSONObject("user");
String response = jObject.getString("profile_pic_url_hd");
return response;
}
Then create an internal class inside your Activity named getDataFromUrl, the class should look like this:
private class getDataFromUrl extends AsyncTask<String, Void, String> {
Context mContext;
public getDataFromUrl(Context mContext) {
this.mContext = mContext;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(arg0[0]);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
return jsonStr;
} else {
Log.e(TAG, "Couldn't get json from server.");
return null;
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
String url = s.replace('|',',').split(",")[1];
Log.d(TAG, "Link: " + url);
}
}
Now you are able to get an user profile picture URL, as a bonus I'll let you the code for using that URL and get the picture in to an ImageView.
Step 2: main_activity.xml
Add an image view, the ImageView, can be custom but I recommend 1:1 scale to keep image quality. Your xml should look like this:
<ImageView
android:id="#+id/imgProfilePic"
android:scaleType="fitXY"
android:src="#drawable/ic_image_black_24dp"
android:layout_width="300dp"
android:layout_height="300dp"/>
</RelativeLayout>
Step 3: DownloadImageTask.java
Now you need to create an external class called DownloadImageTask the full code have to be like this one:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import java.io.InputStream;
public 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);
}
}
Step 4: MainActivity.java
Now the last step will be to add your Main Activity the piece of code to load the ImageView, this code will be in your OnCreate void inside your MainActivity.java
ImageView = thumbnails = (ImageView)findItemById(R.id.imgProfilePic);
new DownloadImageTask(thumbnails).execute(getHDProfilePicFromUsername("jusnjshb"));
That's all, hope it helps I have 2 years using this code hope it helps.
Related
I have some EditTexts in my Activity.First text the for the title, second is for author.Now the user loose focus from the second edittext ie author.I want to get the images related to that content (title and author).So what I did, I concat the title and author name and make HTTP request using Volley.And I print that response.But the response is so unpredictable that I can not fetch the images from it.
try {
String googleImageUrl = "http://images.google.com/images?q=";
String query = URLEncoder.encode(title + " " + author, "utf-8");
String url = googleImageUrl + query;
Toast.makeText(context, url, Toast.LENGTH_SHORT).show();
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
post_des.setText("Response is: " + response);
Log.i("Show me something awesome dude", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
post_des.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
And the responce is like this:
Response is: <!doctype html><html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><link href="/images/branding/product/ico/googleg_lodp.ico" rel="shortcut icon"><title>something something - Google Search</title><style>#gb{font:13px/27px Arial,sans-serif;height:30px}#gbz,#gbg{position:absolute;white-space:nowrap;top:0;height:30px;z-index:1000}#gbz{left:0;padding-left:4px}#gbg{right:0;padding-right:5px}#gbs{background:transparent;position:absolute;top:-999px;visibility:hidden;z-index:998;right:0}.gbto #gbs{background:#fff}#gbx3,#gbx4{background-color:#2d2d2d;background-image:none;_background-image:none;background-position:0 -138px;background-repeat:repeat-x;border-bottom:1px solid #000;font-size:24px;height:29px;_height:30px;opacity:1;filter:alpha(opacity=100);position:absolute;top:0;width:100%;z-index:990}#gbx3{left:0}#gbx4{right:0}#gbb{position:relative}#gbbw{left:0;position:absolute;top:30px;width:100%}.gbtcb{position:absolute;visibility:hidden}#gbz .gbtcb{right:0}#gbg .gbtcb{left:0}.gbxx{display:none........like wise
I was expecting to get a Html doc.
So how to make a HTTP request for images with the content(title and author).
Edit
In layman language,
Suppose I am on images.google.com, and I typed in something in search bar, and make a search, now I want the data that Google return as the Url of the images on that webpage(I am doing all this in backend not showing it to the user.)
I think it is now understandable :)
You got html but of the whole search page. You can retrieve pictures' urls with css selectors and [JSOUP library][2] (easy to use). Just go to Chrome browser and then choose Settings - More tools - Developer tools. Then click the right mouse button on a picture and choose inspect and you'll see which container is for the pictures and what div contains src url of the images and then you right click this div and choose copy css selector. Then work with the library.
But be aware, it's not practical cause if they change the page html your code will beak. You better use specific api for this purpose, like Google Custom Search API as it was suggested in comments above.
To put image into UI you need to get its url address and then you can use Glide or Picasso or even Volley
// Retrieves an image with Volley specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
mImageView.setImageResource(R.drawable.image_load_error);
}
});
EDIT:
Here is CSS selector for all images on the google search page img.rg_ic. Using Jsoup and this selector you'll get access to all the image tags on the page
Jsoup example:
Document doc = Jsoup.connect(your link string).get();
Elements imgs = doc.select("img");//the selector
for (Element img : imgs) {
//add img urls to String array and then use to get imgs with them
String s = img.attr("src");
arr.add(s);
}
[![enter image description here][3]][3]
EDIT2 :
Your code with changes:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
TextView textView;
String googleImageUrl = "https://www.google.co.in/search?biw=1366&bih=675&tbm=isch&sa=1&ei=qFSJWsuTNc-wzwKFrZHoCw&q=";
ArrayList<String> urls = new ArrayList<>();
String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncTask.execute(new Runnable() {
#Override
public void run() {
Log.i("someething" , "something");
getImages("https://www.google.co.in/search?biw=1366&bih=675&tbm=isch&sa=1&ei=qFSJWsuTNc-wzwKFrZHoCw&q=somethingsomething");
}
});
}
private void getImages(String url) {
Document doc = null;
try{
doc = Jsoup.connect(url).get();
}catch (IOException e){
e.printStackTrace();
}
Elements imgs = doc.select("img");
System.out.println("Damn images"+imgs);
for (Element img : imgs){
Log.d("image-src", img.attr("data-src"));//changed `src` to `data-src`
}
}
}
You can get a List of google search images using Jsoup .،. see official site here https://jsoup.org/
/**
* Extract images from google as ArrayList.
*
* #param searchQuery is the string to search for
* #return returnedURLS is the List of urls
*/
private List<String> extractImagesFromGoogle(String searchQuery) throws IOException {
final String encodedSearchUrl = "https://www.google.com/search?q=" + URLEncoder.encode(searchQuery, "UTF-8") + "&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiUpP35yNXiAhU1BGMBHdDeBAgQ_AUIECgB";
Document document = Jsoup.connect(encodedSearchUrl).get();
String siteResponse = document.toString();
List<String> returnedURLS = new ArrayList<String>();
// parse the object and query the values (the urls) for specific keys ("ou")
Pattern pattern = Pattern.compile("\"ou\":\"(.*?)\"");
Matcher matcher = pattern.matcher(siteResponse);
while (matcher.find()) {
returnedURLS.add(matcher.group(1));
}
return returnedURLS;
}
// Test it now:
List<String> retrievedURLS = new ArrayList<String>();
try {
retrievedURLS = extractImagesFromGoogle("pyramids");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(">> List Size: " + retrievedURLS.size());
System.out.println(">> List of images urls: " + retrievedURLS);
I am trying to retrieve photo url from Firebase and convert it to bitmap. Uploading to Firebase and retrieving images from Firebase storage to show in listview is no problem because Glide library is converting the url and placing the image into an ImageView. For a bitmap texture in OpenGL though, Glide isn't appropriate. The photo url comes out like this (key information has been altered to preserve security but the format is the same):
https://firebasestorage.googleapis.com/v0/b/api-project-123456789000.appspot.com/o/jzL123yO1ZxTZ1Se4ldYk2YD92v1%2Ffull%2F1234572738682%2Fcropped.jpg?alt=media&token=1e6cad76-1234f-4f9d-8923-7af07015da7d
In the logcat it shows the retrieved url. In that format though. I save the url to with SharedPreferences. I am using the Rajawali OpenGL framework to show a 3d model which gets it's bitmap from the image url that was saved to SharedPreferences. I use this async class to convert the url to bitmap and apply it to the 3d models texture.
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import org.rajawali3d.materials.textures.Texture;
import org.rajawali3d.materials.textures.TextureManager;
public class AsyncLoadImageTask extends AsyncTask<String, String, Bitmap> {
private final static String TAG = "AsyncTaskLoadImage";
private Texture texture;
public AsyncLoadImageTask(Texture texture) {
this.texture = texture;
}
#Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
try {
URL url = new URL(params[0]);
bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
texture.setBitmap(bitmap);
TextureManager.getInstance().replaceTexture(texture);
}
}
I then call this in my renderers onRender() method:
try {
//This is reference to the String containing the url
String url = UserPrefs.getCustomLabel(mContext);
new AsyncLoadImageTask(labelTex).execute(url);
} catch (Exception e) {
e.printStackTrace();
}
}
When the Rajawali framework tries to apply the converted bitmap, it says that the url string is null/empty.
This is the referenced method from UserPrefs.getCustomLabel():
public static String getCustomLabel(Context context){
SharedPreferences settings;
String text;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
text = settings.getString(LABEL_KEY, null); //2
Log.d(UP_TAG, "Retrieved Url: " + text);
return text;
}
There is a very good chance that Rajawali is making an unauthenticated request to Firebase Storage. If you use the default security rules, that unauthenticated request will be denied.
The common solution is to use a so-called download URL to retrieve the image. A download URL is a publicly accessible, but unguessable, URL. You can get such URL from a Firebase Storage URL by using this code snippet from the Firebase documentation on downloading files:
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
So I'm currently investigating the possibility to replace a lot of custom image loading AsyncTasks in a project, with the Picasso library and it seems promising. However there is one issue that I'm not completely sure how to solve using Picasso.
In this case we are downloading Album Art for music tracks, however when our ListView is to be shown we only have the track ids. So first we have to lookup an Album Art URL based on the track id and then load it into an ImageView. Currently we have an AsyncTask where we in doInBackground() first lookup the image URL and then load a Bitmap from it, which is then pasted to onPostExecute.
Is there any way to make this pre-lookup using Picasso or will we have to wrap the Picasso call in a AsyncTask that first perform the lookup (and feels like it kinda defeats the purpose).
Update: How it works now:
private class AlbumArtTask extends AsyncTask<String, Bitmap, Bitmap> {
#Override
protected Bitmap doInBackground(String... strings) {
final String id = strings[0];
// This part is what I'm asking for, basically to be able to make an Async
// lookup of the url that will later be used by Picasso
final Uri uri = getAlbumArtUriFromTrackId(id);
// Creating the Bitmap and return it to be used in an ImageView
// This part is handled by Picasso
return bitmap = createBitmapFromUri(uri);
}
#Override
protected void onPostExecute(Bitmap bitmap) {
// load image into ImageView
}
}
Update 2: Made up example of what I'm after
Picasso.with(mContext)
.load(new UriProvider() {
// Get the actual image URI here
public Uri getImageUri() {
return getAlbumArtUriFromTrackId(id);
}
})
// And load it here
.into(mImageView);
I had to solve the same problem for my app. I used OkHTTP interceptor to redirect the request.
This is my picassa declaration:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new PhotoInterceptor());
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttpDownloader(client))
.build();
This is a basic implementation of the interceptor:
public class PhotoInterceptor implements Interceptor {
Gson gson = new Gson();
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.isSuccessful()) {
Photo photo = gson.fromJson(response.body().charStream(), Photo.class);
if (photo!=null) {
request = request.newBuilder()
.url(photo.getPhoto_file_url())
.build();
response = chain.proceed(request);
}
}
return response;
}
}
You have to load images from your Album Art URL using Piccaso while your listView is created. Then on Detail screen of Track you have to call your actual image url to set it in imageview.
Edited
private class AlbumArtTask extends AsyncTask<String, Bitmap, Uri> {
ImageView mImageView;
Context mContext;
public AlbumArtTask(Context context,ImageView imageView){
this.mImageView=imageView;
this.mContext=context;
}
#Override
protected Uri doInBackground(String... strings) {
final String id = strings[0];
// This part is what I'm asking for, basically to be able to make an Async
// lookup of the url that will later be used by Picasso
final Uri uri = getAlbumArtUriFromTrackId(id);
// Creating the Bitmap and return it to be used in an ImageView
// This part is handled by Picasso
return uri;
}
#Override
protected void onPostExecute(Uri uri) {
// You have to pass imageview in constructor.
// load image into ImageView
Picasso.with(mContext).load(uri)/* And load it here*/.into(mImageView);
}
}
In my Android App I download text-content from my website and store it in my mysql database. Now I want to download and cache images from the same website. With AndroidVolley's <com.android.volley.toolbox.NetworkImageView, downloading normal pictures works pretty good. But I want to download SVG-vector images, cache them and display in my App. So far this is not possible in Android Volley...
I already tried to download the SVGs with AndroidVolley as a String and then put them in a svg-android element (See here), but svg-android did never show my image. It seems, that it can't display SVGs created by Inkscape...
Does anybody know a simple way, how to download these files and display them in a view?
Thanks
// UPDATE 27.3.2015 //
So this is my solution:
With Android Volley I set a StringRequest to access my SVG-Image as a String. The AndroidSVG library (don't mix up with the svg-android library) may convert this String into a SVG-Object and put it into an SVGImageView-View. Here is an example Code how it worked:
StringRequest stringRequest = new StringRequest("http://******/image.svg",new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
SVG svg = SVG.getFromString(response);
View view = getView();
if(view != null){
SVGImageView target = (SVGImageView)view.findViewById(catID);
target.setSVG(svg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(Globals.TAG,"Fehler "+error);
}
});
RequestQueue queue = Volley.newRequestQueue(mContext);
stringRequest.setShouldCache(true);
queue.add(stringRequest);
Thanks a lot!
Here is a slightly different solution because I wanted to store the image loaded in a variable for reuse several times.
So I just changed the source Volley replacing "private" with "protected" for the method
protected Response <Bitmap> doParse (NetworkResponse response)
Then I created a class that inherits from ImageRequest and which uses the AndroidSVG library.
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.widget.ImageView;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGParseException;
/**
* Version modifiée de ImageRequest de Volley pour gérer aussi les images SVG
*/
public class MultiFormatImageRequest extends ImageRequest {
final String TAG = "EE." + getClass().getSimpleName();
boolean isSVG = false;
public MultiFormatImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
ImageView.ScaleType scaleType, Bitmap.Config decodeConfig, Response.ErrorListener errorListener) {
super(url, listener, maxWidth, maxHeight, scaleType, decodeConfig, errorListener);
String extension = url.substring(url.lastIndexOf("."));
isSVG = (extension.toUpperCase().contains("SVG"));
}
/****
* ATTENTION : "private" Response in Volley need to be changed in protected
*/
#Override
protected Response<Bitmap> doParse(NetworkResponse response) {
Bitmap image = null;
if (isSVG) {
try {
SVG svg = SVG.getFromString(new String(response.data));
int h = (int) svg.getDocumentHeight();
int w = (int) svg.getDocumentWidth();
image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444); // transparent
Canvas bmcanvas = new Canvas(image);
svg.renderToCanvas(bmcanvas);
} catch (SVGParseException ex) {
ex.printStackTrace();
}
if (image == null) {
return Response.error(new ParseError(response));
} else {
return Response.success(image, HttpHeaderParser.parseCacheHeaders(response));
}
} else
return super.doParse(response); // Volley default
}
}
You can try two things:
1. With svg-android library, you need to set
imgView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
to make it work.
2. If the first approach doesn't work, you can try using SVGImageView.
I first make an async task to retrieve JSON data. One of the JSON objects retrieved is an URL.
// This goes on my asynctask to get JSON data from the server
#Override
public void onPostExecute(JSONObject json) {
try {
String title = json.getString("title");
String description = json.getString("description");
String url = json.getString("url");
// Here comes the tricky part
Bitmap bitmap = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
new DownloadBitmap(bitmap).execute(url);
myArrayList.add(new DataContainer(title,description,bitmap));
// End of the tricky part
}catch(Exception e) {}
adapter.NotifyDataSetChange();
}
OK as you can see I need a reference of bitmap to send to the downloader class and to the arraylist for the listview's adapter.
The problem is that the downloader class goes like this:
class DownloadBitmap extends AsyncTask<the 3 paramateres goes here>{
Bitmap bitmap;
public DownloadBitmap(Bitmap b) {
//Here I have a reference to the SAME bitmap object I added to the arraylist
bitmap = b;
}
#Override
protected void doInBackground(String... Params) {
// some code.....
// THIS IS THE PROBLEM, it re-initialize.
bitmap = BitmapFactory.decodeStream(inputstream);
}
I wasn't able to find any other way around, and If possible I'd like a solution library independant, my company has strict policy over software dependance.
You can use Universal Image Loader Library.
link