12

我的应用程序拍照,我想在 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("Test", "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 发送。

我尝试使用此链接示例 -在 instagram 中分享照片- 但没有用。

4

3 回答 3

24

我解决了我的问题。

我在 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("Test", "last picture (" + c1.getString(1) + ") taken on: " + new Date(c1.getLong(2)));
}

Log.i("Image path", "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 verifyInstagram(){
    boolean installed = false;

    try {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.instagram.android", 0);
        installed = true;
    } catch (NameNotFoundException e) {
        installed = false;
    }
        return installed;
    }
于 2013-06-02T20:06:37.033 回答
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"));
于 2017-04-18T12:11:43.457 回答
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 设备。

于 2019-01-18T12:55:32.150 回答