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.