【问题标题】:"Unable to find image" when sharing using Instagram使用 Instagram 分享时“找不到图片”
【发布时间】:2019-03-05 00:16:35
【问题描述】:

在我的应用程序中,用户可以选择在 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.TITLE, MediaStore.Images.ImageColumns.DATE_TAKEN};
        Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");


        shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
        shareIntent.setPackage("com.instagram.android");

        c1.close();
        startActivity(shareIntent);

问题是它会在应用程序上显示一条消息

无法加载图片。

我在 manifest.xml 中添加了此权限

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我还确保从我的设备中获得许可,但没有任何效果。

【问题讨论】:

    标签: android android-intent instagram


    【解决方案1】:

    我解决了我的问题。我删除了这行代码

            Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
    

    我替换了这个

        shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
    

    有了这个

    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photoPath, "img", "Identified image")));
    

    【讨论】:

      【解决方案2】:

      试试这个代码

      String type = "image/*";
      String filename = "/myPhoto.jpg";
      String mediaPath = Environment.getExternalStorageDirectory() + filename;
      
      createInstagramIntent(type, mediaPath);
      
      private void createInstagramIntent(String type, String mediaPath){
      
          // Create the new Intent using the 'Send' action.
          Intent share = new Intent(Intent.ACTION_SEND);
      
          // Set the MIME type
          share.setType(type);
      
          // Create the URI from the media
          File media = new File(mediaPath);
          Uri uri = Uri.fromFile(media);
      
          // Add the URI to the Intent.
          share.putExtra(Intent.EXTRA_STREAM, uri);
      
          // Broadcast the Intent.
          startActivity(Intent.createChooser(share, "Share to"));
      }
      

      支持的图片格式为 jpeg、gif、png

      【讨论】:

      • 这只会为我打开一个“分享”菜单。我将 .jpg 添加到我的代码中,但它仍然无法正常工作
      猜你喜欢
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多