在我的应用程序使用中,我想将 Instagram 个人资料图片放入他/她的帐户中。
如何以编程方式从 Instagram 获取用户的个人资料图片?
如:
https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN
)您将收到如下回复:
{
"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
}
}
然后您可以使用
profile_picture
并使用诸如Facebook Fresco 之类的工具下载它,然后它将为您显示图像。
POST instagram 个人资料图片,您不能,instagram 尚未提供用于发布照片的 post 方法,但是您可以查看和下载根据哈士奇的回答。
JSON
数据。
https://apinsta.herokuapp.com/u/USERNAME
id
。
ID
访问此链接
https://i.instagram.com/api/v1/users/ID_HERE/info/
url
对象中的
hd_profile_pic_url_info
键。
ImageView
将图像加载到
Glide
中。
快乐编码!
感谢 Chrisby 的服务器。
第1步:MainActivity.java
getHDProfilePicFromUsername 的字符串函数。该函数应如下所示:
/// 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;
}
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);
}
}
第2步:main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imgProfilePic"
android:scaleType="fitXY"
android:src="@drawable/ic_image_black_24dp"
android:layout_width="300dp"
android:layout_height="300dp"/>
</RelativeLayout>
第3步:下载ImageTask.java
现在您需要创建一个名为 DownloadImageTask 的外部类。完整的代码必须像这样:
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);
}
}
第4步:MainActivity.java
现在最后一步是添加主活动,即加载 ImageView 的代码。此代码将位于您的MainActivity.java 文件中的 OnCreate void 中:
ImageView = thumbnails = (ImageView)findItemById(R.id.imgProfilePic);
new DownloadImageTask(thumbnails).execute(getHDProfilePicFromUsername("jusnjshb"));