Android: post image and text to Instagram

这一生的挚爱 提交于 2019-12-04 13:27:58

Try this it is worked for me. I will use it for share my product details. i will get details of product like name,image,description from the server and display in app and then share it to instagram.

Get image url from server.

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);

Convert Uri String to 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;

}

Convert Bitmap to 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);

}

slayton

I doubt the Instagram developers will ever release iPhone like hooks for android as Intents already serve this purpose.

If you want to share an image from your app use an intent like this:

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"));

Note that this will show a dialoge allowing the user to pick which photo sharing app to launch.

This works for me.... but it assumes the package name of com.instagram.android, which may change without notice I suppose.

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);
}

You could always check to see if the com.instagram.android package is installed using getPackageManager() ... and if not, then don't set the package name in the intent and let the app chooser help the user pick the app to share the image with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!