androidshare-intent

Intent share not working with image and text sharing


I am using share intent in my application,but i am not able to share image and text,i am using image and text from my json response,but it is not working.i am not getting any error,but the the method for sharing is not working

JSON Response : http://pastie.org/10753346

public void onShareItem(View v) {
        // Get access to bitmap image from view

        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(descpic);
        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.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
        }
    }

    // Returns the URI path to the Bitmap displayed in specified ImageView
    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();
    }*/

    OutputStream fOut = null;
    Uri outputFileUri=null;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "folder_name" + File.separator);
        root.mkdirs();
        File sdImageMainDirectory = new File(root, "myPicName.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later.",
                Toast.LENGTH_SHORT).show();
    }

    try {
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }
    return outputFileUri;
}

Solution

  • Do this to save image. Replace your method with this

    public Uri getLocalBitmapUri(ImageView imageView) {
        imageview.buildDrawingCache();
        Bitmap bm = imageview.getDrawingCache();
    
        OutputStream fOut = null;
        Uri outputFileUri;
        try {
            File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "folder_name" + File.separator);
            root.mkdirs();
            File imageFile = new File(root, "myPicName.jpg");
            outputFileUri = Uri.fromFile(imageFile);
            fOut = new FileOutputStream(imageFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        try {
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            return outputFileUri;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }