其他分享
首页 > 其他分享> > 通过Android Intent共享位图

通过Android Intent共享位图

作者:互联网

在我的Android应用程序中,我有一个位图(比如说b)和一个按钮.现在当我点击按钮时,我想分享位图.我在onClick()中使用下面的代码来实现这个目的: –

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

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

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

我哪里出错了?

解决方法:

我找到了解决方案的2个变种.两者都将Bitmap保存到存储中,但图像未显示在库中.

第一个变种:

保存到外部存储

>但是应用程序的私人文件夹.
>对于API< = 18,它需要许可,而对于较新的则不需要.
1.设置权限

在标记之前添加到AndroidManifest.xml中

<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);
        image.compress(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.检查存储可访问性

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

/**
 * Checks wheather 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.它不需要任何许可.

1.在AndroidManifest.xml中设置FileProvider

<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="http://schemas.android.com/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);
        image.compress(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;
}

有关文件提供商 – https://developer.android.com/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,android-intent,intentfilter,bitmap
来源: https://codeday.me/bug/20190926/1822054.html