我怎样才能在Instagram上发布图片和文字?
如果必须打开Instagram应用程序或者通过API或其他方式进行操作都没有问题。
问题在于我找不到如何操作:他们只有iPhone钩子,而且API也难以理解。
是否有人已经成功做到了这一点或者知道该怎么做呢?
谢谢。
我尝试过这个方法,它对我有用。我将使用它来分享我的产品详情。我将从服务器获取产品名称、图片和描述等详细信息,在应用程序中显示后分享到 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();
}
在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);
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;
}
将位图转换为 Uri
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);
}
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"));
这对我来说是有效的...但它假设了包名为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包,如果没有,则不要在意图中设置包名称,让应用程序选择器帮助用户选择与之共享图像的应用程序。