问题
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 :
回答1:
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.
回答2:
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.
回答3:
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 inhd_profile_pic_url_info
object.Load the image in
ImageView
usingGlide
.
Happy coding !
Credit to Chrisby for his server. https://stackoverflow.com/a/49862390/9565955
回答4:
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.
来源:https://stackoverflow.com/questions/41494863/how-to-get-instagram-profile-picture