我正在开发动画 Gif 应用程序,图像是从 Web 下载并存储在手机内存中的。然后所有图像都在我的应用程序中以动画形式完美显示,但问题是当我通过安装的手机应用程序共享它时。它不共享。图片路径没问题,还是无法分享。我使用以下代码进行分享。请帮忙。
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;
}
imagePath 获取正确的路径,即如下所示
/sdcard/emulated/0/Android/data/PackageName/files/FolderName/xyz.gif
当'file'转换为bytes时,图片的bytes[]变为(0),图片甚至在App中被销毁,不再共享。
最佳答案
//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"));
}
关于android - 如何通过 Intent Android 从外部存储分享 gif 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51346496/