在 Instagram 中发布图片
Posted
技术标签:
【中文标题】在 Instagram 中发布图片【英文标题】:post image in Instagram 【发布时间】:2013-04-30 09:41:22 【问题描述】:问题:在我的应用中,我需要像 FB 或 Twitter 一样。
我已经做了什么:登录并从 Instagram 获取照片到我自己的应用程序。但无法在 Instagram 上发布图片。
【问题讨论】:
【参考方案1】:然而,在 Instagram 上发布图片是不可能的,比如 FB 或 twitter。
但这是使用已安装的 Instagram 实现此目的的另一种方法,如果没有,这将通知用户下载应用程序。
public void onClick(View v)
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null)
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), imagePath, "I am Happy", "Share happy !")));
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
else
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
startActivity(intent);
【讨论】:
请告诉我如何在图片中添加文字? @DennySharma 您可以在图片中添加标题然后发布,请参阅链接***.com/questions/11100428/… 那么有什么回调方法可以从instagram intent 中获取发布的照片url 吗?像 startactivityforresult() P.S:我目前被禁止在 *** 上提问,只是因为我删除了几个问题。抱歉在这里提问。 对于那些想要为图像添加标题的人,他们可以通过在开始活动之前添加此行来实现:shareIntent.putExtra(Intent.EXTRA_TEXT," the caption"); 这是在后台全自动工作还是只启动应用程序?我需要上传多张照片。【参考方案2】:不,你不能。引用自Instagram API docs:
目前,无法通过 API 上传。我们有意识地选择不添加它,原因如下:
-
Instagram 是关于您在旅途中的生活 - 我们希望鼓励在应用程序内拍摄照片。不过,未来我们可能会根据具体情况为个别应用提供白名单访问权限。
我们要打击垃圾邮件和低质量照片。一旦我们允许从其他来源上传,就很难控制进入 Instagram 生态系统的内容。尽管如此,我们正在努力确保用户在我们的平台上获得一致的高质量体验。
更新:但是,如果您使用的是 ios(尽管您标记了 Android),则可以通过以下方式“发送”照片(实际上,它会在 Instagram 中打开图像)自定义 URL 方案。见this。
【讨论】:
谢谢.. 节省了我的时间:-D 我们可以通过安装在我们手机上的Instagram来实现吗? Android 应用 Instagram 也是 Android 上的共享目标,因此您可以使用标准 SEND Intent 方法从任何应用传递图像。 www.autogrammer.com 是如何做到这一点的? 如何使用instagram api发表评论?【参考方案3】:试试这个链接:
你可以使用这个类上传图片到instagram。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.codec.binary.Hex;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class InstagramPostHelper
private static InstagramPostHelper instance = null;
protected InstagramPostHelper()
public static InstagramPostHelper getInstance()
if (instance == null)
instance = new InstagramPostHelper();
return instance;
private String GenerateSignature(String value, String key)
String result = null;
try
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(value.getBytes());
byte[] hexBytes = new Hex().encode(rawHmac);
result = new String(hexBytes, "UTF-8");
catch (Exception e)
return result;
private static final String COOKIES_HEADER = "Set-Cookie";
public static java.net.CookieManager msCookieManager = new java.net.CookieManager();
private HttpsURLConnection conn;
private static String TextUtilsJoin(CharSequence delimiter, List<HttpCookie> list)
StringBuilder sb = new StringBuilder();
boolean firstTime = true;
for (Object token: list)
if (token.toString().trim().length()<1) continue;
if (firstTime)
firstTime = false;
else
sb.append(delimiter + " ");
sb.append(token);
return sb.toString();
private String GetJSONStringAndPostData(String jsonaddress,String postdata,String agent)
String sonuc = "";
try
byte[] postDataBytes = postdata.toString().getBytes("UTF-8");
URL url = new URL(jsonaddress);
conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setRequestProperty("User-Agent", agent);
//Set Cookies
if(msCookieManager.getCookieStore().getCookies().size() > 0)
conn.setRequestProperty("Cookie", TextUtilsJoin(";", msCookieManager.getCookieStore().getCookies()));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
if (conn.getResponseCode() != 200)
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
//Get Cookies
Map<String, List<String>> headerFields = conn.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if(cookiesHeader != null)
for (String cookie : cookiesHeader)
msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null)
sonuc += output;
conn.disconnect();
catch (MalformedURLException e)
return "";
catch (IOException e)
return "";
return sonuc;
public void SendImage(String Caption,byte[] ImageByteArray) throws UnsupportedEncodingException, ParseException
String Agent = "Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; en_US)";
String Guid = java.util.UUID.randomUUID().toString();
String DeviceId = "android-" + Guid;
String Data = "\"device_id\":\"" + DeviceId + "\",\"guid\":\"" + Guid + "\",\"username\":\"MYUSERNAME\",\"password\":\"MYPASSWORD\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=UTF-8\"";
String Sig = GenerateSignature(Data, "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5");
Data = "signed_body=" + Sig + "." + URLEncoder.encode(Data, "UTF-8") + "&ig_sig_key_version=4";
if (msCookieManager.getCookieStore()!= null)
msCookieManager.getCookieStore().removeAll();
//Login Request
String login = GetJSONStringAndPostData("https://instagram.com/api/v1/accounts/login/", Data,Agent);
JSONParser parser = new JSONParser();
Object obj = parser.parse(login);
JSONObject jsonObject = (JSONObject) obj;
if (((String) jsonObject.get("status")).equals("ok")) //Login SuccessFul
//Login Successful
System.out.println("Login Successful !");
//Post Image
String upload = "";
try
final HttpMultipartHelper http = new HttpMultipartHelper(new URL("https://instagram.com/api/v1/media/upload/"));
http.addFormField("device_timestamp", Long.toString((new Date()).getTime()));
http.addFilePart("photo", ImageByteArray);
final byte[] bytes = http.finish();
upload = new String(bytes);
catch (IOException e)
e.printStackTrace();
System.out.println(upload);
obj = parser.parse(upload);
jsonObject = (JSONObject) obj;
if (((String) jsonObject.get("status")).equals("ok")) //Login SuccessFul
String mediaID = (String) jsonObject.get("media_id");
Data = "\"device_id\":\"" + DeviceId + "\",\"guid\":\"" + Guid + "\",\"media_id\":\"" + mediaID + "\",\"caption\":\"" + Caption + "\",\"device_timestamp\":\"" + Long.toString((new Date()).getTime()).substring(0,10) + "\",\"source_type\":\"5\",\"filter_type\":\"0\",\"extra\":\"\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=UTF-8\"";
Sig = GenerateSignature(Data, "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5");
Data = "signed_body=" + Sig + "." + URLEncoder.encode(Data, "UTF-8") + "&ig_sig_key_version=6";
//Login Request
System.out.println(Data);
String result = GetJSONStringAndPostData("https://instagram.com/api/v1/media/configure/", Data,Agent);
System.out.println(result);
else //Login UnsuccessFul
System.out.println("Login Unsuccessful !" + login);
https://gist.github.com/ecdundar/d5b6bdcc2035448fc9cd
【讨论】:
【参考方案4】:看来我们现在可以做到了,更多信息您可以查看official docs here
【讨论】:
它仍然使用意图共享提要。请参阅此代码。 Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");【参考方案5】:试试这个
public void ShareInsta()
File dir = new File(Environment.getExternalStorageDirectory(), "FolderName");
File imgFile = new File(dir, "0.png");
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("image/*");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imgFile));
sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
sendIntent.setPackage("com.instagram.android");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try
startActivity(Intent.createChooser(sendIntent, "Share images..."));
catch (android.content.ActivityNotFoundException ex)
Toast.makeText(SaveAndShareActivity.this, "Please Install Instagram", Toast.LENGTH_LONG).show();
【讨论】:
以上是关于在 Instagram 中发布图片的主要内容,如果未能解决你的问题,请参考以下文章