我在我的 Android 应用程序中使用了 Instagram 集成。 我的要求是将多张图像共享到一篇文章中。 这是 Instagram 本身的新功能。
据我所知 Instagram 仅支持意图解析来进行数据共享。
Intent instagram = new Intent(android.content.Intent.ACTION_SEND);
instagram.setType("image/*");
instagram.putExtra(Intent.EXTRA_STREAM, uri);
instagram.putExtra(Intent.EXTRA_TEXT, "caption:goes here");
instagram.setPackage("com.instagram.android");
startActivity(instagram);
我尝试对多个 uri 使用 parceable arraylist。
Instagram 不支持帖子(Feed)中的多流。只是卷轴或故事。 您可以在here查看 Feed 的文档。 在这里查看故事。
我对Reels和Stories的多个图像
的实现try {
val share = Intent(Intent.ACTION_SEND_MULTIPLE)
share.type = "image/*" //For a video "video/*"
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, listUri)
share.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.startActivity(
Intent.createChooser(
share,
context.getString(R.string.share)
)
)
} catch (e: Exception) {
//Not installed Instagram open GP or dialog
}
对于 使用一张图像供稿
try {
val share = Intent(Intent.ACTION_SEND)
share.type = "image/*" //For a video "video/*"
putExtra(Intent.EXTRA_STREAM, uri)
share.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.startActivity(
Intent.createChooser(
share,
context.getString(R.string.share)
)
)
} catch (e: Exception) {
//Not installed Instagram open GP or dialog
}
发送到Instagram Feed图像的方式。 您需要将所有图像保存在图库中并将单张照片发送到 Instagram,然后用户自己选择图像
以及使用 Glide 从远程图像获取 Uri 的其他帮助 另外,将文本复制到剪贴板
Glide.with(context)
.asBitmap()
.load(imageUrl)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
getLocalBitmapUri(resource)?.let {
createInstagramIntent(it)
}
}
override fun onLoadCleared(placeholder: Drawable?) {}
})
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
val clip = ClipData.newPlainText("label", message)
clipboard?.setPrimaryClip(clip)
fun getLocalBitmapUri(bmp: Bitmap): Uri? {
return try {
val bmpUri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val file = File(
context.filesDir.path,
"share_image_" + System.currentTimeMillis() + ".png"
)
FileOutputStream(file)
.use {
bmp.compress(Bitmap.CompressFormat.PNG, 100, it)
}
FileProvider.getUriForFile(context, context.packageName + ".fileprovider", file)
} else {
val file = File(
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"share_image_" + System.currentTimeMillis() + ".png"
)
FileOutputStream(file).use {
bmp.compress(Bitmap.CompressFormat.PNG, 100, it)
}
Uri.fromFile(file)
}
bmpUri
} catch (e: IOException) {
e.printStackTrace()
null
}
}
For multiple share image on single post For Ios side it is possible
using : https://www.npmjs.com/package/react-native-share
this node package for android by default is not provide multiple select image share on single post
only for possible on ios side
for multiple share here is below code
in the android side android does not support multiple image share on feeds on Instagram due to Instagram private
----> on face book you can share multiple images as a post below same code work on facebook also (Android and 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
}
};