Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to share image that was loaded from a remote url?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.47k
    Comment on it

    Hello Nerds, This tutorial is for forwarding/sharing image from one application to other which was loaded from a remote url. Assuming that you have loaded an image from remote url and displayed it in your ImageView. The basic procedure is by taking bitmap from a view and sharing it with the help of intents. Two simple methods need to implement, first is getLocalBitmapUri() which returns URI path and next is onShareItem() triggered for sharing image to the other application.

    public void onShareItem(View v) {
    
        // Get access to bitmap image from view
        ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
    
        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(ivImage);
    
        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 {
            // user did'n get bitmap uri, sharing failed
        }
    }
    
    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 {
    
            // This way, you don't need to request external read/write permission.
            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 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: