我正在尝试分享图像和文本。 当我这样做时:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screenshot.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
share.putExtra(android.content.Intent.EXTRA_TEXT, textToShare);
startActivityForResult(Intent.createChooser(share, "Share Image"),
FROM_SHARE);
它适用于所有应用程序(从弹出对话框中选择,如 Gmail),除了 facebook,facebook 只接受图像而不接受文本。 有什么办法可以将带有图像的额外文本传递到 Facebook 应用程序吗? 谢谢!
FACEBOOK 中有一个错误,我也有同样的问题,如果你真的想这样做,无论如何你需要使用 facebook SDK 并使用 Facebook ShareDialog。
节点包:https://www.npmjs.com/package/react-native-share
// 用于将本机 AAP 反应到 Instagram 上的多个图像共享,因此只有 ios 端您可以共享多个选择的图像
in the android side android does not support multiple image share on feeds on Instagram due to Instagram private
----> 在 Facebook 上,您可以在下面的帖子中分享多个图像,同样的代码也可以在 Facebook 上使用(Android 和 ios)
here is a code for multiple share on ios side
const shareMultipleImage = async () => {
try {
const instagramAvailable = Platform.OS == 'android' ? await checkPackage() : await checkInstagramInstalled()
if (!instagramAvailable) {
const url = Platform.OS === 'android'
? 'https://play.google.com/store/apps/details?id=com.instagram.android&hl=en&gl=US'
: 'https://apps.apple.com/in/app/instagram/id389801252';
if (await Linking.canOpenURL(url)) {
Linking.openURL(url);
}
return; // Stop execution if Instagram is not available
}
let localFilePaths = [];
for (let i = 0; i < images.length; i++) {
const { uri } = images[i];
if (uri.startsWith('file:///')) {
localFilePaths.push(uri);
} else {
const localFilePath = `${RNFS.CachesDirectoryPath}/image${i + 1}.jpg`;
const downloadOptions = {
fromUrl: uri,
toFile: localFilePath,
};
try {
await RNFS.downloadFile(downloadOptions).promise;
localFilePaths.push('file://' + localFilePath);
} catch (error) {
console.log('Error downloading file:', error);
// Handle error (e.g., skip this image)
continue;
}
}
}
const shareOptions = {
urls: localFilePaths,
type: 'image/*',
};
await Share.open(shareOptions);
} catch (error) {
console.log('Error sharing to Instagram:', error);
if (error && error.message === "User did not share") {
const url = 'https://www.instagram.com/';
const supported = await Linking.canOpenURL(url);
if (supported) {
Linking.openURL(url);
} else {
console.log('Instagram not installed', 'Please install Instagram to continue.');
}
}
}
};
// for single share is support both side android and ios
here is a code for single share
const shareImage = async (uri) => {
try {
let instagramAvailable = false;
if (Platform.OS === 'android') {
instagramAvailable = await checkPackage();
} else if (Platform.OS === 'ios') {
instagramAvailable = await checkInstagramInstalled();
}
if (!instagramAvailable) {
// Instagram is not available, redirect to the respective app store
const url = Platform.OS === 'android'
? 'https://play.google.com/store/apps/details?id=com.instagram.android&hl=en&gl=US'
: 'https://apps.apple.com/in/app/instagram/id389801252';
if (await Linking.canOpenURL(url)) {
Linking.openURL(url);
}
} else {
// Instagram is installed, proceed with sharing the image
const shareOptions = {
url: uri.startsWith('file:///') ? uri : `file://${RNFS.CachesDirectoryPath}/image.png`,
type: 'image/*',
failOnCancel: false
};
if (!uri.startsWith('file:///')) {
await RNFS.downloadFile({ fromUrl: uri, toFile: shareOptions.url }).promise;
}
await Share.open(shareOptions);
}
} catch (error) {
console.error('Error sharing to Instagram:', error);
// Handle error as needed
}
};