There is a dialogFragment in which the test result is displayed. It has a Share button. How to add the ability to click on this button to send to different services (social networks, VibER, etc.) the screenshot of this dialogFragment itself , the accompanying text and the link to the application in Google Play?

  • This is a very general question, it’s necessary to write a whole program and hardly anyone will do it for you. - pavlofff
  • @pavlofff I thought maybe someone has a ready-made solution. - Maxim Fomichev
  • @metalurgus I found this right away, thanks - Maxim Fomichev

2 answers 2

  1. You can take a screenshot with the kl View like this :

     public static Bitmap loadBitmapFromView(View v, int width, int height) { Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); v.draw(c); return b; } 
  2. Next, the resulting Bitmap can be pushed into the Intent , which launches the system sharing dialog one of these methods (did not penetrate, but they seem to suggest that the resulting Bitmap first written to a file).

    Way times :

     String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null); 

    Method two :

     File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File imageFile = new File(path, getCurrentTime()+ ".png"); FileOutputStream fileOutPutStream = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream); fileOutPutStream.flush(); fileOutPutStream.close(); String url = "file://" + imageFile.getAbsolutePath(); 
  3. Now we form and launch Intent sharinga:

     final Intent intent = new Intent( android.content.Intent.ACTION_SEND); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url)); intent.putExtra(Intent.EXTRA_TEXT, "ТУТ_КАКОЙ_ТО_ТЕКСТ"); intent.setType("image/png"); startActivity(Intent.createChooser(intent, "Share with Friends")); 

PS

Maybe you can’t just transfer both the image and the text for all services. Separately, text and image can be transferred. But all sorts of tweets will hardly accept images. To work correctly with sharing in to-l specific services it is best to use their SDK

  • @MaksimFomichev, -1 in this case is a constant. whether match_parent or wrap_content . Just try view.getHeight() - JuriySPb
  • Yes, thanks, I just wanted to write what needed to be done this way, the screenshot was received and sent, now it's up to the text with the link - Maxim Fomichyov
  • A photo is sent to the vibeer, a photo and text are sent to the post office, and for example nothing is eaten on Facebook - Maxim Fomichev
  • @MaksimFomichev, with FaceBook, judging by Google, you have to deal with them through the SDK. As far as I know, there is no standard acceptance for heterogeneous content for all services. The text is something that everyone should accept, but something more will always have its own nuances. - Yuriy SPb
  • one
    Thank! Then we will stop on the text with the link! - Maxim Fomichev

Faced the same need - send a photo (or rather several) and a description to it from the application. And there was the same problem as Maxim Fomichev.

How to deal with this.

I chose the sending method via intent , without using the API social networks, mailers and instant messengers (I’ll call them just messengers).

Ie, I transfer the data through Intent and I call the list of applications capable of processing several types of data (see the answer YuriySPb ), in my case these are images and text. Then it is up to the user and his choice of the messenger.

I decided to test how the sending is processed in different messengers.

Messengers with processing both types of data at the same time turned out to be much less than those that can work with text and pictures but apart from each other.

Further, not everything is so colorful. Since even those messengers who "declare" by implicit intents, they have the functionality to process several types of data at the same time, if they are processed in a very different way, and sometimes even in some way.

For some, text editors that appear next to the picture being sent do not automatically accept the text that we transmitted along with the picture in the intente , and perhaps only enter it with pens each time or copy-paste (also with pens).

Not for the purpose of advertising or anti-advertising, but only due to the fact that they are free and installed on my phone, take for example the well-known Viber, Skype, Hangouts and Gmail. They are good each in its own way, but are they suitable for my tasks.

The back end of my research:

Several uri images flooded in ArrayList .

 ArrayList<Uri> imageUris = new ArrayList<Uri>(); 

Then, naturally, having received all the necessary permission , I call:

 private void openMultiDataSender() { String description = "some text"; Intent sendMultiData = new Intent(); sendMultiData.setAction(Intent.ACTION_SEND_MULTIPLE); sendMultiData.putExtra(Intent.EXTRA_STREAM, imageUris); sendMultiData.putExtra(Intent.EXTRA_TEXT, description); sendMultiData.setType("*/*"); startActivity(sendMultiData); } 

The use of any particular MIME type did not lead to the desired result, just like trying to combine them. Therefore I transfer sendMultiData.setType("*/*"); .

What we have as a result of the transfer of images and text:

Skype - opens the editor with an empty field for adding a comment and attached pictures. Further, if you want, write with pens or paste the text you need from the buffer, if you added it programmatically, of course.

Viber - opens the editor with an empty field for adding a comment and attached pictures. If the picture for the transfer is ONE - then the picture and the text that we transmitted will go. If there are many pictures, only pictures will go. (By the way, a maximum of 10 pictures). Comments to pictures like Skype - we write with pens.

Hangouts - opens a window with a submit button and attached pictures. By clicking on the send button, the chat window opens and we see how the pictures are sent, and lo and behold, inconspicuous (if there is not enough text) in the field for entering a message there is already our text, you just need to press the send button again. (Why a miracle, yes, because I noticed him from the 3rd time, and just before that I closed the chat and returned to the initial application). But it is still better than nothing.

Gmail - the editor is opened with the text filled with ours and pictures in the attachments (restriction only on the total weight of the pictures). It remains to choose the recipient and send.

In the process of solving this problem, I considered several options to get out of such an uncomfortable situation.

First option. To generate intents sequentially, first with text, then with a picture (or vice versa). But in this case there is a risk of transmitting incomplete data.
The reason for this:
1) The difficulty of obtaining confirmation that the first operation was successful, before starting the second. For example, the user will switch to some instant messenger, which is beyond our control, and there they will simply refuse to transmit data.

2) Suppose that the first action was nevertheless carried out and our picture or text (depending on which we send the first one) safely flew away to the addressee. Nevertheless, there remains the risk that the user may not confirm the 2nd action (not everyone likes to send some information in two visits, try to convince someone that they need to go to the post office 2 times to send one package).
In this case, the addressee risks getting only the first part of what we were going to send.

So you need to write pop-up messages or dialog boxes with instructions to the user how to send the data set we need. Time-consuming and confusing can get code.

The second option: Knowing that the pictures are accurately transmitted, you can make a picture from the text we need and send it along with the rest of the pictures.

The third option: Most instant messengers support the transfer of PDF files. Alternatively, one PDF file can be made from text and pictures / pictures. Well, or another text document that supports images in the body. But it is rather laborious and hardly takes less time for coding than the first option with all the dialog boxes and resubmitting data.

I can not yet offer more optimal solutions, besides, I assume that there are already some libraries ready to send combined data, but I don’t know what they would like to fix.