【问题标题】:Sharing current view via ACTION_SEND without storing the image in android通过 ACTION_SEND 共享当前视图而不将图像存储在 android
【发布时间】:2017-01-05 20:10:24
【问题描述】:

我想使用 ACTION_SEND 分享应用的当前视图。

将当前视图转换为位图并将其存储在外部存储中以将位图转换为可解析Uri的方法需要权限。

片段:

    public static Bitmap getScreenShot(View view) {
        View screenView = view.getRootView();
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        return bitmap;
    }

    // View to BitMap
    Bitmap b  = getScreenShot(getWindow().getDecorView().findViewById(android.R.id.content));

    //BitMap to Parsable Uri (needs write permissions)
    String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b,"title", null);
    Uri bmpUri = Uri.parse(pathofBmp);

    //Share the image
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/jpeg");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "send"));

这需要 WRITE_EXTERNAL_STORAGE 权限, 有没有办法在不将其存储在 Android 上的外部存储(无权限)的情况下执行相同的操作?

【问题讨论】:

    标签: java android share android-permissions android-sharing


    【解决方案1】:

    如果您的minSdkVersion为19或更高,您可以使用getExternalFilesDir()将图像写入外部存储并避免权限问题。

    您可以将文件写入内部存储(例如,getCacheDir()),然后使用FileProvider 共享它。无论如何,您需要使用FileProvider 或一些ContentProvider,如Android 7.0+ does not like the file scheme

    如果您想避免磁盘 I/O... 您必须将Bitmap 压缩为ByteArrayOutputStream,然后编写您自己的ContentProvider 以使用该byte[] 提供服务。这有点冒险,因为如果您的进程在其他应用程序结束尝试使用Uri 之前终止,那么您就不走运了,因为您的位图已经消失了。

    【讨论】:

    • 有没有其他方式(除了view->bitmap->storage/contentprovider)可以在app中转换当前view并分享给其他app?
    • @Rishi:不是这样。您可以使用 Android 5.0+ 上的媒体投影 API 作为屏幕截图部分。
    • 感谢您对我所有问题的回答,我在使用 fileProvider 共享内存中的图像时遇到问题,接收应用程序总是询问权限,您能评论一下吗? stackoverflow.com/questions/41615290/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2022-07-21
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多