与 URL android 共享意图共享图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25135873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:07:34  来源:igfitidea点击:

share image with URL android share intent

androidandroid-intent

提问by Amanni

I need to know if it is possible to share an image using only its url with a share intent. Here is my code.

我需要知道是否可以仅使用具有共享意图的 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);

So far its not working and I haven't found any helpful answers online. I would like to do this using the share intent and without downloading the image.

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

回答by Nitin Misra

You can share image using share intent, but you've to decode image to a localized Bitmap

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

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..."));

loadedImageis the loaded bitmap from http://eofdreams.com/data_images/dreams/face/face-03.jpg

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

回答by Amanni

See here 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
                }

then add this to your activity :

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

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;
}

回答by QArea

convert url to string format

将 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, String.valueOf(imageUri));
startActivity(imageIntent);

回答by sathish mobapp dev

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"));