jan*_*oth 9 android share image android-intent
我有一个从 url 共享图像的应用程序。上次 android 更新,当我想在 instagram 提要中共享图像时,我收到了来自 instagram 的消息“无法加载图像”。
但我可以分享图片故事、直接消息和任何地方......我只有 instagram 提要有这个问题。
public void onShareItemOreo() {
// Get access to bitmap image from view
imageView = (ImageView) findViewById(R.id.thumbnail);
// Get access to the URI for the bitmap
Uri bmpUri = prepareShareIntent(imageView);
if (bmpUri != null) {
//outfile is the path of the image stored in the gallery
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setData(bmpUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT,marketLink);
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
//
}
}
public Uri prepareShareIntent(ImageView imageView) {
// Fetch Bitmap Uri locally
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
Uri bmpUri = getBitmapFromDrawable(bmp);// see previous remote images section and notes for API > 23
// Construct share intent as described above based on bitmap
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
return bmpUri;
}
Run Code Online (Sandbox Code Playgroud)
更新: 此问题已在本周早些时候发布的 Instagram 版本中修复。不再需要变通方法。
上面提到的所有解决方案都不适合我,因为似乎Instagram 应用程序中的更改破坏了通过ContentProvider
或其衍生工具的直接共享FileProvider
。
我确实注意到共享MediaStore
内容 Uri 仍然有效,因为在共享之前写入 MediaStore 的其他应用程序(例如 Google 照片)仍然能够共享图像以供馈送。
您可以将图像File
的MediaStore
如下:
@SuppressLint("InlinedApi")
fun insertImageToMediaStore(file: File, relativePath: String): Uri? {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, file.name)
val mimeType = when (file.extension) {
"jpg", "jpeg" -> "jpeg"
"png" -> "png"
else -> return null
}
put(MediaStore.Images.Media.MIME_TYPE, "image/$mimeType")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath)
put(MediaStore.MediaColumns.IS_PENDING, 1)
}
}
val collection = when (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
true -> MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
false -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}
val uri = contentResolver.insert(collection, values)
uri?.let {
contentResolver.openOutputStream(uri)?.use { outputStream ->
try {
outputStream.write(file.readBytes())
outputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
values.clear()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
values.put(MediaStore.Images.Media.IS_PENDING, 0)
contentResolver.update(uri, values, null, null)
}
} ?: throw RuntimeException("MediaStore failed for some reason")
return uri
}
Run Code Online (Sandbox Code Playgroud)
然后Uri
,您返回后,通过 Intent 共享,如下所示:
val filePath = "/data/data/io.jammy.withintent/files/IMG-20200321_093350_2020-122758.jpg" // this is an example path from an app-internal image file
val context: Context? = this
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
insertImageToMediaStore(File(filePath), "Pictures/Your Subdirectory")?.let { uri ->
val clipData = ClipData.newRawUri("Image", uri)
intent.clipData = clipData
intent.putExtra(Intent.EXTRA_STREAM, uri)
val target = Intent.createChooser(intent, "Share Image")
target?.let { context?.startActivity(it) }
} ?: run {
Log.e(TAG, "Unsupported image file")
return
}
Run Code Online (Sandbox Code Playgroud)
虽然这并不理想,因为随后将图像写入MediaStore
,这在许多情况下可能不是理想的行为,但它重新启用了在中期分享的能力,同时 Instagram 修复了他们的 whoopsie。
好的,我搜索并找到了解决方案。我不知道这是正确的方法,但解决了我的问题..
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2430 次 |
最近记录: |