如何从链接分享图像,即,无需下载图像,只需使用按钮分享

Ash*_*sim 5 android android-intent android-sharing android-glide

我是 android studio 的初学者,我认为这很容易,但我也不明白

如何从链接共享图像,即,无需下载图像,只需在社交媒体上共享,例如 whats 应用程序。我可以下载图像,然后共享,然后将其删除,但我不想这样做。我正在使用 glide 库在图像视图中加载图像,然后当用户单击一个按钮时,它只会共享其在图像视图中显示的图像以及我拥有的链接,然后返回到应用程序。

我正在使用 Glide 库来加载图像。

任何帮助将不胜感激。

提前致谢。

小智 5

首先,您需要在 glide 中加载图像(因为您正在使用 glide ),然后您可以将其共享到任何地方,但图像将保存到存储中

从 glide 加载图像的代码(图像正在保存到存储中,您可以稍后删除它)

       Glide.with(getApplicationContext())
                    .load(imagelink)   \\link of your image file (url)
                    .asBitmap().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)

                    .into(new SimpleTarget<Bitmap>(250, 250) {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {


                            Intent intent = new Intent(Intent.ACTION_SEND);
                            intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
                            String path = MediaStore.Images.Media.insertImage(getContentResolver(), resource, "", null);


                            Uri screenshotUri = Uri.parse(path);



                            intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                            intent.setType("image/*");

                            startActivity(Intent.createChooser(intent, "Share image via..."));
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();


                            super.onLoadFailed(e, errorDrawable);
                        }

                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            Toast.makeText(getApplicationContext(), "Starting", Toast.LENGTH_SHORT).show();

                            super.onLoadStarted(placeholder);
                        }
});
Run Code Online (Sandbox Code Playgroud)

如果需要,可以通过添加更多代码从存储中删除该图像