我尝试从网址显示图片,到目前为止,我已经成功了但是当我尝试从用户的网址加载Instagram个人资料图片时,它无法正常工作。 我想知道如何使用Instagram API获取用户的图像并使用毕加索将其加载到imageview中。
答案 0 :(得分:0)
实际上,我使用的方法不需要访问令牌,您唯一需要的就是用户名。我将下面的代码留给您
步骤1:MainActivity.java
第一个getHDProfilePicFromUrl
///This function will return to you the url of the user profile picture
private String getHDProfilePicFromUrl(String url) throws ExecutionException, InterruptedException, JSONException {
JSONObject jObject;
String parsingUsername,username;
if(url.contains("?"))
{
parsingUsername = url.substring(0,url.lastIndexOf("?"))+"/";
}
else{
parsingUsername = url;
}
username = getBetweenStrings(parsingUsername,".com/","/");
String profileInfo = new getDataFromUrl(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; }
第二个getBetweenStrings
private String getBetweenStrings( String text, String textFrom, String textTo) {
String result = "";
result = text.substring( text.indexOf(textFrom) + textFrom.length(), text.length());
result = result.substring(0,result.indexOf(textTo));
return result;
}
然后在您的Activity中创建一个名为getDataFromUrl的内部类,该类应如下所示:
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);
}
}
现在,您可以获取用户个人资料图片的URL,此外,我将为您提供使用该URL的代码,并将图片导入ImageView。
第2步:main_activity.xml
添加图像视图ImageView可以自定义,但我建议使用1:1比例以保持图像质量。您的xml应该如下所示:
<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步:DownloadImageTask.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
现在,最后一步将是向您的Main Activity添加一段代码以加载ImageView,该代码将位于MainActivity.java中的OnCreate空隙中
String url="https://www.instagram.com/juanjshb/";
ImageView = thumbnails = (ImageView)findItemById(R.id.imgProfilePic);
new DownloadImageTask(thumbnails).execute(getHDProfilePicFromUrl(url));
仅此而已,希望对我有帮助2年,希望使用此代码对我有帮助。