2

I am developing Animated Gif App, the images are downloaded from Web and stored in Phone memory. Then all the images are perfectly shown in animated form in my App, but the problem is when I Share it via the installed Apps of phone. it doesn't share. The path of the image is OK, and still I am unable to share it. I use the following code for sharing. Please help.

public static boolean shareImage(Context context, String imagePath) {
    File file = new File(imagePath);

    try {
        byte[] readData = new byte[1024 * 500];
        FileInputStream fis = new FileInputStream(file);

        FileOutputStream fos = new FileOutputStream(file);
        int i = fis.read(readData);

        while (i != -1) {
            fos.write(readData, 0, i);
            i = fis.read(readData);
        }

        fos.close();
    } catch (IOException io) {
        showToast(context, io.getLocalizedMessage());
    }      

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    String dataString = "MY_DATA_STRING";

    Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);
    intent.setDataAndType(uri, dataString);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    intent.setType("image/gif");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    context.startActivity(Intent.createChooser(intent, context.getString(R.string.shareStickerTitle)));
    return true;
}

The imagePath gets the right path i.e something like below

/sdcard/emulated/0/Android/data/PackageName/files/FolderName/xyz.gif

When the 'file' converted to bytes, then the bytes[] of the image become (0), and the image destroyed even in the App and not shared.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Zia Ur Rahman
  • 1,850
  • 1
  • 21
  • 30
  • Simplest and lightest way is passing **Address** of it to dest activity. – Hassan Sadeghi Jul 15 '18 at 08:12
  • What do you mean with 'it does not share'? What happens instead? – greenapps Jul 15 '18 at 09:19
  • 1
    You have nonsense code. You read from and write to the same file. – greenapps Jul 15 '18 at 09:22
  • Just like sharing images via intent but they are located in drawable folder which can be shared perfectly, but in my case all the images are located in phone storage, so I get the Address/path of that image, and want to share it via intent. – Zia Ur Rahman Jul 15 '18 at 10:34
  • And may be I am wrong in my Code, so please post/edit the actual code that how can a gif animated image be shared please. A png or jpg images are shared perfectly and even the animated gif can also be shared but if it located in drawable folder inside my App, but the problem is getting gif image from storage and then share, so how to do this? – Zia Ur Rahman Jul 15 '18 at 10:35
  • Please share your own code/idea, how it be possible to get image path (which is located in storage) and then share it via intent. – Zia Ur Rahman Jul 15 '18 at 10:39
  • OK thanks , I got the solution ... it is very simple ... No need any inputstreams etc... just pass the filepath to Uri Fileprovider is enough ... and it works. So simple, but i was confuse. Now got the solution. – Zia Ur Rahman Jul 15 '18 at 11:42
  • Yes indeed. You only had to remove some lines. – greenapps Jul 15 '18 at 14:00
  • @greenapps, But can you please help, that how to Save this image (which is stored in external storage) into Photo Gallery. Or simply copy image from Storage to Gallery. – Zia Ur Rahman Jul 15 '18 at 18:10
  • Switch your device off/on and it will be in the gallery. – greenapps Jul 15 '18 at 18:12
  • this is not the solution actually, the gif image should be stored at run time and will be displayed as well. how to do that. – Zia Ur Rahman Jul 16 '18 at 10:17

3 Answers3

1
//Share Gif
public void shareGif(Context context, String fileName) {

    String baseDir = Environment.getExternalStorageDirectory().toString() + "/YourFolderName/";

    File file = new File(baseDir + "/" + fileName + ".gif");

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    String dataString = "MY_DATA_STRING";

    Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);
    intent.setDataAndType(uri, dataString);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    intent.setType("image/gif");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    context.startActivity(Intent.createChooser(intent, "Share Emoji"));
}
0

The problem is on input and output stream. You read and write in the same file. Hope you will get help from this link. This is the right way to share gif file using intent.

Your try-catch block should be as like below.

 try {
    byte[] readData = new byte[1024*500];
    InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));

    FileOutputStream fos = new FileOutputStream(file);
    int i = fis.read(readData);

    while (i != -1) {
        fos.write(readData, 0, i);
        i = fis.read(readData);
    }

    fos.close();
} catch (IOException io) {
}
Mafujul
  • 1,090
  • 10
  • 15
0
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";
File sharingGifFile = new File(baseDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/gif");
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
        BuildConfig.APPLICATION_ID + ".provider", sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM,shareuri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '23 at 07:58