问题描述
有没有使用方法 Intent.ACTION_SEND
共享,而无需截图 android.permission.WRITE_EXTERNAL_STORAGE
?
Is there a way to use Intent.ACTION_SEND
to share a screenshot without requiring android.permission.WRITE_EXTERNAL_STORAGE
?
这里的一部分份额:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooserIntent = Intent.createChooser(shareIntent, shareTitle);
startActivity(chooserIntent);
本股工作正常时,URI指向文件的 getExternalFilesDir()
,但我preFER不需要的<$ C的解决方案$ C> WRITE_EXTERNAL_STORAGE 隐私,关心用户的权限。
The share works fine when the uri points to a file in getExternalFilesDir()
, but I'd prefer a solution that does not require the WRITE_EXTERNAL_STORAGE
permission for privacy-concerned users.
我试过3种不同的方法:
I've tried 3 different approaches:
-
文件提供:
File Provider:
uri = FileProvider.getUriForFile(context, authority, imageFile);
这适用于一些市场份额的风格(如Gmail),但不是别人(谷歌+)。
This works for some share styles (Gmail) but not others (Google+).
上传到Web服务器:
uri = Uri.parse("http://my-image-host.com/screenshot.jpg");
这失败随处可见,有些崩溃(谷歌+)。
This fails everywhere, crashing some (Google+).
(我怀疑这的可能的工作,如果我实现了使用每个社交网络的API,而不是共享的逻辑我自己在 chooserIntent
)
(I suspect this could work if I implemented sharing logic myself using per-social-network APIs instead of the chooserIntent
)
注入媒体商店:
uri = MediaStore.Images.Media.insertImage(contentResolver, bitmap, name, description);
这将抛出一个SecurityException解释它需要WRITE_EXTERNAL_STORAGE。
This throws a SecurityException explaining it requires WRITE_EXTERNAL_STORAGE.
是我缺少的还有其他办法?
Are there other approaches I'm missing?
推荐答案
由斯特凡Rusek基于,我创建 LegacyCompatCursorWrapper
,旨在帮助改善 FileProvider
的兼容性(和其他的ContentProvider
实现)与正在寻找 _DATA
列,没有发现它们的客户端应用程序。由 MediaStore
最初使用的 _DATA
模式,但它从来没有对应用程序尝试引用该列是个好主意
Based on work by Stefan Rusek, I created LegacyCompatCursorWrapper
, designed to help improve compatibility of FileProvider
(and other ContentProvider
implementations) with client apps that are looking for _DATA
columns and not finding them. The _DATA
pattern was used originally by MediaStore
, but it was never a good idea for apps to try referring to that column.
要会同 FileProvider
使用它,将作为相关性,然后创建自己的 FileProvider
的子类,像这样的:
To use it in conjunction with FileProvider
, add my CWAC-Provider library as a dependency, and then create your own subclass of FileProvider
, such as this one:
/***
Copyright (c) 2015 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.cp.v4file;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;
public class LegacyCompatFileProvider extends FileProvider {
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder)));
}
}
这一切做的就是将 FileProvider
查询()
结果在 LegacyCompatCursorWrapper
。您的应用程序配置的其余部分是相同使用 FileProvider
直接(例如,&LT;元数据&GT;
元素) ,除了你的&LT;活性GT;
元素的的android:名称
属性将指向你自己的类。您可以在看到这个动作。
All this does is wrap the FileProvider
query()
results in a LegacyCompatCursorWrapper
. The rest of your app configuration would be identical to using FileProvider
directly (e.g., <meta-data>
element), except that your <activity>
element's android:name
attribute would point to your own class. You can see this in action in this sample app.
这篇关于没有WRITE_EXTERNAL_STORAGE分享图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!