android share image from url

前端 未结 10 1826
攒了一身酷
攒了一身酷 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!

    0 讨论(0)
  • 2020-12-01 08:51

    An adapted version of @eclass's answer which doesn't require use of an ImageView:

    Use Picasso to load the url into a Bitmap

    public void shareItem(String url) {
        Picasso.with(getApplicationContext()).load(url).into(new Target() {
            @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("image/*");
                i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                startActivity(Intent.createChooser(i, "Share Image"));
            }
            @Override public void onBitmapFailed(Drawable errorDrawable) { }
            @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
        });
    }
    

    Convert Bitmap into Uri

    public Uri getLocalBitmapUri(Bitmap bmp) {
        Uri bmpUri = null;
        try {
            File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            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;
    }
    
    0 讨论(0)
  • 2020-12-01 08:51

    I use these codes from this tutorial

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

    then add your application manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-12-01 08:52

    First you need to load image in the glide. Then you can share it to anywhere. Code to load image from glide (image is being saved to storage, you can delete it later).

    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);
       Log.i("quoteswahttodo", "is onresoursereddy" + path);
    
       Uri screenshotUri = Uri.parse(path);
    
       Log.i("quoteswahttodo", "is onresoursereddy" + screenshotUri);
    
       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);
      }
     });
    
    0 讨论(0)
提交回复
热议问题