【问题标题】:share image with URL android share intent与 URL android 共享意图共享图像
【发布时间】:2023-04-03 04:29:01
【问题描述】:

我需要知道是否可以仅使用带有共享意图的 URL 来共享图像。这是我的代码。

Intent imageIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://eofdreams.com/data_images/dreams/face/face-03.jpg");
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(imageIntent);

到目前为止,它不起作用,我还没有在网上找到任何有用的答案。我想使用共享意图而不下载图像来执行此操作。

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您可以使用共享意图共享图像,但您必须将图像解码为本地化位图

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
    String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
    Uri screenshotUri = Uri.parse(path);
    
    intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    intent.setType("image/*");
    startActivity(Intent.createChooser(intent, "Share image via..."));
    

    loadedImage 是从http://eofdreams.com/data_images/dreams/face/face-03.jpg 加载的位图

    【讨论】:

    • 那么没有位图就没有办法做到这一点?
    • 我的“loadedImage”可以是网址的字符串吗?
    • 像魅力一样工作! :D
    • 任何可能的直接分享?没有下载图片?
    • loadedImage 怎么样?我们如何在上面加载位图?
    【解决方案2】:

    请看这里HERE

    final ImageView imgview= (ImageView)findViewById(R.id.feedImage1);
    
                    Uri bmpUri = getLocalBitmapUri(imgview);
                    if (bmpUri != null) {
                        // Construct a ShareIntent with link to image
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                        shareIntent.setType("image/*");
                        // Launch sharing dialog for image
                        startActivity(Intent.createChooser(shareIntent, "Share Image"));    
                    } else {
                        // ...sharing failed, handle error
                    }
    

    然后将其添加到您的活动中:

    public Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable){
           bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
           return null;
        }
        // Store image to default external storage directory
        Uri bmpUri = null;
        try {
            File file =  new File(Environment.getExternalStoragePublicDirectory(  
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }
    

    【讨论】:

    • 这种分享图片的方式太多余了
    • 而不是 OP 的要求
    【解决方案3】:

    将网址转换为字符串格式

    Intent imageIntent = new Intent(Intent.ACTION_SEND);
    Uri imageUri = Uri.parse("http://eofdreams.com/data_images/dreams/face/face-03.jpg");
    imageIntent.setType("image/*");
    imageIntent.putExtra(Intent.EXTRA_STREAM, String.valueOf(imageUri));
    startActivity(imageIntent);
    

    【讨论】:

      【解决方案4】:
      Intent intent = new Intent(Intent.ACTION_SEND);
      
      intent.setType("text/plain");
      
      intent.putExtra(Intent.EXTRA_TEXT,"http://eofdreams.com/data_images/dreams/face/face-03.jpg"); 
      
      startActivity(Intent.createChooser(intent, "Share Image"));                   
      

      【讨论】:

      • 我已将您的答案标记为代码,以便于阅读;但是您介意添加一些解释,以便人们更容易了解您的解决方案是什么以及它是如何工作的吗?
      • 这只是发送图片链接,不是图片本身
      • @HarpreetSingh 某些应用会打开 URL 到图像中某些应用只会显示 url
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      相关资源
      最近更新 更多