如何使用我的Android应用程序将照片发送到Instagram?

时间:2013-05-31 19:04:07

标签: android share photo instagram

我的应用程序拍照,我想在Instagram上分享。

我的应用将图片保存在此目录中

File storagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram");

现在我正试图通过这段代码获取我在Instagram上分享的最后一张照片

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");

final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");

if (c1.moveToFirst() ) {

    Log.i("Teste", "last picture (" + c1.getString(0) + ") taken on: " + new Date(c1.getLong(1)));
}

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram/" + c1.getString(0)));
shareIntent.setPackage("com.instagram.android");

c1.close();

startActivity(shareIntent);

我收到一条Toast,显示此错误消息“无法下载文件”。 这个Toast由Instagram发送。

我尝试使用此链接示例 - share a photo in instagram - 但无效。

请帮帮我!!!

3 个答案:

答案 0 :(得分:19)

我解决了我的问题。

我在camera.takePicture之后添加了这一行。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

此行执行“刷新”,并在手机识别出手机中保存的新闻照片后。

我对我的方法进行了一些修改

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");                 

final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
    MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.TITLE, MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");

if (c1.moveToFirst() ) {
    Log.i("Teste", "last picture (" + c1.getString(1) + ") taken on: " + new Date(c1.getLong(2)));
}

Log.i("Caminho download imagem", "file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/"  + c1.getString(1) + ".png");

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1)+".png"));
shareIntent.setPackage("com.instagram.android");

c1.close();

startActivity(shareIntent);

使用另一种方法,我验证Instagram是否已安装在手机上

private boolean verificaInstagram(){
    boolean installed = false;

    try {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.instagram.android", 0);
        installed = true;
    } catch (NameNotFoundException e) {
        installed = false;
    }
        return installed;
    }

答案 1 :(得分:1)

将此代码放入按钮单击侦听器中,它会将您重定向到应用程序并确保您的设备已安装Instagram应用程序。

String type = "image/*";
imageview.buildDrawingCache();
Bitmap bmap = imageview.getDrawingCache();
Uri bmpUri = getLocalBitmapUri(bmap);
Intent share = new Intent(Intent.ACTION_SEND);
if (Utils.isPackageExisted(this,"com.instagram.android")) {
 share.setPackage("com.instagram.android");
}
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, bmpUri);
startActivity(Intent.createChooser(share, "Share to"));

答案 2 :(得分:0)

尝试以下代码:

File mFileImagePath = " /storage/emulated/0/Image Editor/Media/FilterImages/Image_Editor_1547816365839.jpg  ";  // Just example you use file URL

private boolean checkAppInstall(String uri) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        //Error
    }

    return false;
}


private void shareInstagram(File mFileImagePath) {
    Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
    if (intent != null) {
        Intent mIntentShare = new Intent(Intent.ACTION_SEND);
        String mStrExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(mFileImagePath).toString());
        String mStrMimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(mStrExtension);
        if (mStrExtension.equalsIgnoreCase("") || mStrMimeType == null) {
            // if there is no extension or there is no definite mimetype, still try to open the file
            mIntentShare.setType("text*//*");
        } else {
            mIntentShare.setType(mStrMimeType);
        }
        mIntentShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFileImagePath));
        mIntentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        mIntentShare.setPackage("com.instagram.android");
        startActivity(mIntentShare);
    } else {
        Toast.makeText(mContext, "Instagram have not been installed.", Toast.LENGTH_SHORT).show();
    }
}

此代码对我有效,并且可在所有Android设备上使用。