How to share image on social networks, android?

3

I'm trying to share an image on social networks, which I get from a URL I tried several ways and I have not succeeded, I use retrofit2 to show the content of the application.

Thank you very much in advance

Here I get the url of the image

           Glide.with(mContext)
            .load(appointment.getmImage())
            .thumbnail(0.5f)
            .override(250, 120)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .bitmapTransform(new RoundedCornersTransformation(mContext, 10, 0)
            )
            .into(holder.imagee);

This is the code I use to share on social networks

btnshared.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(Intent.EXTRA_TITLE, titulocompartir.getText().toString());
                        intent.putExtra(Intent.EXTRA_TEXT, comentariocompartir.getText().toString());
                        imagee.buildDrawingCache();
                        Bitmap image= imagee.getDrawingCache();
                        extras.putParcelable(Intent.EXTRA_STREAM, image);
                        intent.setType("image/*");
                        mContext.startActivity(intent);


                }

            });

social networks will be available depending on the applications you have installed, if normal sharing does only text but the problem comes when I try to share an image does not load it in the post

    
asked by Josee Naava 07.08.2017 в 02:28
source

1 answer

1

This can be done using Intent.createChooser () .

If you can get the Bitmap of the desired image, you have to save it to disk and that way it could be sent using Intent.createChooser () , to save it and later get access to the image, it is necessary to define a FileProvider , you can review in stackoverflow where information related to this is found.

As an example I leave you this tutorial:

First define a provider in your AndroidManifest.xml , this within the <application> tag:

 <application
        ...
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.providers.FileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths" />
        </provider>

    </application>

later it adds the file where the paths of the file will be defined within /res/xml , by default it does not exist / xml, so you must create it. Inside, add the file paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="imagenes/"/>
</paths>

This way you will have permissions to access the file on disk, this is an example using an Asynctask, here you can replace the Bitmap obtaining from a url by obtaining the Bitmap from the ImageView:

imagee.buildDrawingCache();
Bitmap image= imagee.getDrawingCache();

that would be called in this way new sendImageTask().execute(); :

   public class sendImageTask extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {

                        try {
                           URL url = new URL("http://i.stack.imgur.com/oURrw.png");
                            Bitmap imagen = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                            // Salva bitmap a disco.
                            try {

                                File cachePath = new File(getCacheDir(), "imagenes"); //path cache.
                                cachePath.mkdirs(); // Crea directorio si no existe.
                                FileOutputStream stream = new FileOutputStream(cachePath + "/imagen.jpg"); // Escribe imagen.
                                imagen.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                stream.close();

                            } catch (IOException e) {
                                e.printStackTrace();
                            }


                            File imagePath = new File(getCacheDir(), "imagenes"); //obtiene directorio.
                            File newFile = new File(imagePath, "imagen.jpg"); //obtiene imagen.

                            String PACKAGE_NAME = getApplicationContext().getPackageName() + ".providers.FileProvider";

                            Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), PACKAGE_NAME, newFile);

                            if (contentUri != null) {

                                Intent shareIntent = new Intent();
                                shareIntent.setAction(Intent.ACTION_SEND);
                                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
                                shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
                                shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                                startActivity(Intent.createChooser(shareIntent, "Elige una aplicación:"));

                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }

            return null;
        }


    }

with this you can share the image in any social network or installed application that supports the sending of images:

    
answered by 07.08.2017 в 18:33