android share image from url

前端 未结 10 1890
攒了一身酷
攒了一身酷 2020-12-01 07:50

I want to share an image using the code:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse(\"http://stacktoheap.com/images/stac         


        
10条回答
  •  情话喂你
    2020-12-01 08:42

    After a lot of pondering, here is the code that I found to be working for me! I think this is one of the simplest versions of code to achieve the given task using Picasso. There is not need to create an ImageView object.

                Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    bmp = bitmap;
                    String path = MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "SomeText", null);
                    Log.d("Path", path);
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
                    Uri screenshotUri = Uri.parse(path);
                    intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                    intent.setType("image/*");
                    startActivity(Intent.createChooser(intent, "Share image via..."));
                }
    
                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
    
                }
    
                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
    
                }
            };
            String url = "http://efdreams.com/data_images/dreams/face/face-03.jpg";
            Picasso.with(getApplicationContext()).load(url).into(target);
    

    Here, bmp is a class level Bitmap variable and url will be the dynamic Internet url to your image for sharing. Also, instead of keeping the code to share inside the onBitmapLoaded() function, it can also be kept inside another handler function and later called from the onBitmapLoaded() function. Hope this helps!

提交回复
热议问题