【问题标题】:Intent share is not able to attach image意图共享无法附加图像
【发布时间】:2017-09-18 16:29:01
【问题描述】:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "My text");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(http://onman.ir/colorfinder/sample.jpg));
sendIntent.setType("*/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sendIntent, "Share using"));

此代码在与Gmail 共享意图时无法附加图像,并且与仅共享多个图像或视频的Facebook 共享时也会出错。当我使用sendIntent.setType("image/*") 更改类型时,它会打开Facebook 的共享窗口,但文本和图像为空白。

【问题讨论】:

  • 您是否在清单中设置 权限?
  • 是的,这个权限已经被添加了。
  • 你遇到了什么错误??
  • 在 facebook 上分享给我这个错误:please attach only a photo or single video

标签: android share-intent


【解决方案1】:

您可以使用此代码进行分享

     Intent shareIntent = new Intent();

                   shareIntent.setAction(Intent.ACTION_SEND);
                   shareIntent.setType("image/*");
                   shareIntent.putExtra(Intent.EXTRA_TEXT,"abc");
                   shareIntent.putExtra(Intent.EXTRA_STREAM, imagebitmap);
                  startActivity(Intent.createChooser(shareIntent, "Share using"));

但如果你想在 facebook 中做,它不会通过意图工作,你应该使用 facebook sdk 来共享图像和文本

FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                                   .setLink(url)
                                   .setCaption(getString(R.string.fb_share))
                                   .setDescription(getString(R.string.fb_share))
                                   .setPicture(
                                                 "https://scontent-lhr3-1.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/11899955_1616850925232395_9146772907853639083_n.jpg?oh=3dd7da7bf03edee84689d66af2024880&oe=56793D62")
                                   .build();
                     uiHelper.trackPendingDialogCall(shareDialog.present());
                     dismissProgressBar();

在您的代码中,您首先要直接共享图像 url,您需要获取位图,然后共享位图而不是 url

【讨论】:

  • 但 Facebook 是分享意图的选项之一。
  • 但根据 Facebook,您可以按时使用意图共享图像或文本,因此当您想要进行共享时,您应该创建一个自定义共享对话框并为 Facebook 共享编写不同的代码。
  • 仅适用于 Facebook,您编写不同的代码,并且对所有共享应用意图进行扩孔。
【解决方案2】:

私人无效shareImage(){ Intent share = new Intent(Intent.ACTION_SEND);

// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");

// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
        + "/myImage.png";

File imageFileToShare = new File(imagePath);

Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(share, "Share Image!"));

试试这个代码

请注意,当您使用 setType() 方法时,您将启用 Android 过滤哪些应用可以共享您的内容。例如,您正在共享文本或 URL,要显示的适当应用程序可以是 Facebook、消息或电子邮件。如果您要分享图片,合适的应用可以是 Instagram、Snapped 或 Picasa。

【讨论】:

    【解决方案3】:
     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == UPLOADIMAGE && resultCode == RESULT_OK)
        {
            uploadImage.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
    
            if (data !=null)
            {
                imageUri=data.getData();
                imageView.setImageURI(imageUri);
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                cursor.moveToFirst();
                columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                attachmentFile = cursor.getString(columnIndex);
                Log.e("Attachment Path:", attachmentFile);
                URI = Uri.parse("file://" + attachmentFile);
                cursor.close();
    
            }
            else {Toast.makeText(TutorForm.this,"Uploading image failed",Toast.LENGTH_LONG).show();}
    
    
    
    
        }
        super.onActivityResult(requestCode, resultCode, data);
    

    OnClickListner

     Intent emailIntent=new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+ clientEmail));
            emailIntent.putExtra(Intent.EXTRA_TEXT,body);
            emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Tutor Registration from App");
            emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri uri = Uri.parse(imageUri.toString());
            emailIntent.putExtra(Intent.EXTRA_STREAM,URI);
            startActivity(Intent.createChooser(emailIntent,"Send Via..."));
    

    【讨论】:

      【解决方案4】:
      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      shareIntent.setType("image/*");
      
      //set your message
      shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 
      String imagePath = Environment.getExternalStorageDirectory() + 
      File.separator + "image_name.jpg";
      File imageFileToShare = new File(imagePath);
      Uri uri = Uri.fromFile(imageFileToShare);
      shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
      Uri uri = Uri.fromFile(imageFileToShare);
      sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
      

      【讨论】:

      • 请提供有关您的答案的更多信息(解释)。
      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。一个正确的解释would greatly improve 它的长期价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案5】:

      这个对我有用

      Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                  shareIntent.setType("image/jpeg");
      
                  shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
                  shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_message));
      
                  shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(yourimagepath);
      
                  startActivity(Intent.createChooser(shareIntent, "Share Image"));
      

      【讨论】:

      • 有什么区别?如果不想添加任何主题呢?
      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多