2012-07-19 54 views
2

如何发布图像和文本到instagram? 没有问题,如果我必须打开Instagram应用程序,或者如果我通过API或其他。 问题是,我找不到如何:他们只有iphone挂钩,并且api是无法解释的。 有人已经这样做了,或者知道吗? 谢谢。Android:将图像和文字发布到Instagram

回答

5

我怀疑Instagram developers会永远释放像android的钩子的iPhone,因为Intents已经达到了这个目的。

如果你想分享你的应用的图像使用这样的意图:

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("image/jpeg"); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Path/To/Image.jpg")); 
startActivity(Intent.createChooser(i, "Share Image")); 

注意,这将显示一个dialoge允许用户挑选,推出其照片共享应用程序。

+0

这是建议的方法,你应该遵循。否则,您将需要使用Instagram API,推出自己的自定义共享内容。 – 2012-07-19 16:48:34

+0

有没有办法通过手机浏览器触发这种情况? – Ericson578 2012-07-31 04:50:04

+0

如果用户安装了Instagram,那么当用户点击分享按钮时,Instagram将显示在该列表中。如果你想通过移动网站分享,你将不得不使用上述方法使用Instagram API – slayton 2012-07-31 05:10:28

4

这适用于我....但它假定com.instagram.android的包名,它可能会更改,恕不另行通知,我想。

private void shareToInstagram(Uri imageUri){ 
    Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("image/*"); 
    i.putExtra(Intent.EXTRA_STREAM, imageUri); 
    i.setPackage("com.instagram.android"); 
    activity.startActivity(i); 
} 

你总是可以检查是否正在使用getPackageManager()安装com.instagram.android包......如果没有,那就不要在意向设置的包名,让应用程序选择器帮助用户选择应用程序来共享图像。

5

试试这个对我有用。我会用它来分享我的产品细节。我会从服务器上获取名称,图像,描述等产品的详细信息,并在应用中显示,然后将其分享给instagram。

从服务器获取图像URL。

URL url = ConvertToUrl(imgURL); 
    Bitmap imagebitmap = null; 
    try { 
     imagebitmap = BitmapFactory.decodeStream(url.openConnection() 
       .getInputStream()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

Post image and text on instagram。

// post on instagram 
      Intent shareIntent = new Intent(
        android.content.Intent.ACTION_SEND); 
      shareIntent.setType("image/*"); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, 
        getImageUri(HomeActivity.this, result)); 
      shareIntent.putExtra(Intent.EXTRA_SUBJECT, sharename); 
      shareIntent.putExtra(
        Intent.EXTRA_TEXT, 
        "Check this out, what do you think?" 
          + System.getProperty("line.separator") 
          + sharedescription); 
      shareIntent.setPackage("com.instagram.android"); 
      startActivity(shareIntent); 

转换URI字符串到URL

private URL ConvertToUrl(String urlStr) { 
try { 
    URL url = new URL(urlStr); 
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), 
      url.getHost(), url.getPort(), url.getPath(), 
      url.getQuery(), url.getRef()); 
    url = uri.toURL(); 
    return url; 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
return null; 

}

转换位图到乌里

public Uri getImageUri(Context inContext, Bitmap inImage) { 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
String path = MediaStore.Images.Media.insertImage(
     inContext.getContentResolver(), inImage, "Title", null); 
return Uri.parse(path); 

}

+1

此代码适用于图像,我无法发送文字与图像。 – AmeeJoshi 2015-11-18 10:14:46

+0

此代码与图像工作正常...无法发送文本 – 2016-08-02 06:27:30