通过 Android Intent 共享位图

编程入门 行业动态 更新时间:2024-10-10 07:20:56
本文介绍了通过 Android Intent 共享位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的 android 应用程序中,我有一个位图(比如 b)和一个按钮.现在,当我单击按钮时,我想共享位图.我在我的 onClick() 中使用以下代码来实现这一点:-

In my android app, I have a bitmap (say b) and a button. Now when I click on the button, I want to share the bitmap. I am making use of the below code inside my onClick() to achieve this :-

Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/png"); intent.putExtra(Intent.EXTRA_STREAM, b); startActivity(Intent.createChooser(intent , "Share"));

我期待能够处理此意图的所有应用程序的列表,但我什么也没得到.android studio 中没有应用程序列表,也没有任何错误.我的应用程序只是挂了一段时间然后退出.

I was expecting a list of all application which are able to handle this intent but I get nothing. There is no list of apps nor is there any error in android studio. My application just get hanged for sometime and then quits.

我检查了位图,它很好(它不为空).

I have checked the bitmap and it is fine (its not null).

我哪里出错了?

推荐答案

我找到了解决方案的 2 个变体.两者都涉及将位图保存到存储中,但图像不会出现在图库中.

I found 2 variants of the solution. Both involve saving Bitmap to storage, but the image will not appear in the gallery.

保存到外部存储

  • 但是到应用程序的私人文件夹.
  • 对于 API <= 18,它需要许可,对于较新的则不需要.

在标签前添加到AndroidManifest.xml

Add into AndroidManifest.xml before tag

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>

2.保存方法:

/** * Saves the image as PNG to the app's private external storage folder. * @param image Bitmap to save. * @return Uri of the saved file or null */ private Uri saveImageExternal(Bitmap image) { //TODO - Should be processed in another thread Uri uri = null; try { File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "to-share.png"); FileOutputStream stream = new FileOutputStream(file); imagepress(Bitmap.CompressFormat.PNG, 90, stream); stream.close(); uri = Uri.fromFile(file); } catch (IOException e) { Log.d(TAG, "IOException while trying to write file for sharing: " + e.getMessage()); } return uri; }

3.检查存储可访问性

外部存储可能无法访问,因此您应该在尝试保存之前检查它:developer.android/training/data-storage/files

/** * Checks if the external storage is writable. * @return true if storage is writable, false otherwise */ public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }

第二个变体

使用 FileProvider 保存到 cacheDir.它不需要任何权限.

Second variant

Saving to cacheDir using FileProvider. It does not require any permissions.

<manifest> ... <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mydomain.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ... </application> </manifest>

2.将路径添加到 res/xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <paths xmlns:android="schemas.android/apk/res/android"> <cache-path name="shared_images" path="images/"/> </paths> </resources>

3.保存方法:

/** * Saves the image as PNG to the app's cache directory. * @param image Bitmap to save. * @return Uri of the saved file or null */ private Uri saveImage(Bitmap image) { //TODO - Should be processed in another thread File imagesFolder = new File(getCacheDir(), "images"); Uri uri = null; try { imagesFolder.mkdirs(); File file = new File(imagesFolder, "shared_image.png"); FileOutputStream stream = new FileOutputStream(file); imagepress(Bitmap.CompressFormat.PNG, 90, stream); stream.flush(); stream.close(); uri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", file); } catch (IOException e) { Log.d(TAG, "IOException while trying to write file for sharing: " + e.getMessage()); } return uri; }

有关文件提供程序的更多信息 - developer.android/reference/android/support/v4/content/FileProvider

More info about file provider - developer.android/reference/android/support/v4/content/FileProvider

压缩和保存可能很耗时,因此这应该在不同的线程中完成

/** * Shares the PNG image from Uri. * @param uri Uri of image to share. */ private void shareImageUri(Uri uri){ Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setType("image/png"); startActivity(intent); }

更多推荐

通过 Android Intent 共享位图

本文发布于:2023-11-28 08:53:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641745.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   Android   Intent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!