0

I'm trying to share an image but I don't know why I'm failing, could you help me please?

String imageUrl = web.get(position).getImage();
    if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
        imageUrl = "http://" + imageUrl;

    Button button = (Button)rowView.findViewById(R.id.condividi);
    final String finalImageUrl = imageUrl;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
            File file = writebitmaptofilefirst("the image", finalImageUrl );
            Uri path = Uri.fromFile(file);
            intent.putExtra(Intent.EXTRA_STREAM, path );
            Intent send = Intent.createChooser(intent, null);
            context.startActivity(send);
        }
    });

public static File writebitmaptofilefirst(String filename, String source) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(extStorageDirectory + "/temp_images");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    OutputStream outStream = null;


    File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".jpg");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        URL url = new URL(source);
        Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

EDIT

String imageUrl = web.get(position).getImage();
    if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
        imageUrl = "http://" + imageUrl;

    Button button = (Button)rowView.findViewById(R.id.condividi);
    final String finalImageUrl = imageUrl;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
            String file = writebitmaptofilefirst("ndp_image", finalImageUrl);
            //Uri path = Uri.fromFile(file);
            intent.putExtra(Intent.EXTRA_STREAM, file );
            Intent send = Intent.createChooser(intent, null);
            context.startActivity(send);
        }
    });

    return rowView;

}

public static String writebitmaptofilefirst(String filename, String source) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(extStorageDirectory + "/temp_images/");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    OutputStream outStream = null;


    File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".jpg");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        URL url = new URL(source);
        Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file.getAbsolutePath();

}
Pier
  • 794
  • 1
  • 12
  • 27
  • What problem do you get? A crash? If so, please post the LogCat too – user2340612 Feb 13 '16 at 23:44
  • @user2340612 I'm getting no crashes, when I'm about to send the files using a social app It just appears a message like : **Sharing failed, please try again** (in the case of Whatsapp) or **Impossible to upload the image** (in the case of Instagram) – Pier Feb 13 '16 at 23:51
  • Are you sure that you need both the `EXTRA_TEXT` and `EXTRA_STREAM` extras? I guess that you need only the second one – user2340612 Feb 13 '16 at 23:57
  • But I need to share some text with an image – Pier Feb 14 '16 at 00:07
  • maybe [this SO answer](http://stackoverflow.com/questions/8296605/problems-sharing-combined-text-and-image-with-share-intent-on-twitter) can help you – user2340612 Feb 14 '16 at 00:10
  • I've already tried with those options but nothing – Pier Feb 14 '16 at 00:15

1 Answers1

0

Add permissions to your manifest

Remove space from file name (the image). With space you need decode the uri. You should paass the full path return file.getAbsolutePath(). You are just passing the file name.
In the case of file exists your not storing in the same path. You were not included the dictionary. extranlstoragepath+/temp_images/+the image.jpg try to log your file paths. And

File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");

You have missed a / between two paramaters.

Wonderful blogpost about storing image

SreeAndroidDev
  • 141
  • 1
  • 1
  • 11