为Android N编译我遇到了一个问题FileProvider.我需要让用户从相册中选择图像/用相机拍照然后将其裁剪成方形.
我已经成功实现了FileProvider用相机拍摄图像,但是从图库中选择图像时遇到了严重的问题.问题是在库中有很多来自不同地方的文件,例如我有Exception:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/6133-3766/DCIM/100ANDRO/DSC_0035.JPG
Run Code Online (Sandbox Code Playgroud)
所以问题是,我可以把什么放到file_paths.xml来访问任何地方/storage/.我不能依赖于确切的路径,因为可能有来自WhatsApp和类似应用程序的图片,例如WhatsApp图像获取此路径:
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20160821-WA0000.jpg
Run Code Online (Sandbox Code Playgroud)
我已设法解决空路径:
<external-path name="external_storage" path=""/>
其类似于Environment.getExternalStorageDirectory()根据文档.
但仍然无法弄清楚如何处理存储在其中的图像/storage/SOME_DIR/.请帮忙.
我试图允许用户将图像共享到设备上的其他应用程序.该图像位于我的应用程序内部存储区域的files /子目录中.它适用于Gmail,但Facebook和Twitter在响应我的意图时都崩溃了.
编辑:Google+也可以.
以下是相关的代码部分.
在Application.xml中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="org.iforce2d.myapp.MyActivity"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
XML/filepaths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="shared" path="shared"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
以下是我的活动中的共享代码:
File imagePath = new File(getContext().getFilesDir(), "shared");
File newFile = new File(imagePath, "snapshot.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(),
"org.iforce2d.myapp.MyActivity", newFile);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
List<ResolveInfo> resInfos =
getPackageManager().queryIntentActivities(shareIntent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : resInfos) {
getContext().grantUriPermission(info.activityInfo.packageName,
contentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(Intent.createChooser(shareIntent, "Share image..."));
Run Code Online (Sandbox Code Playgroud)
contentUri记录时的值是:
content://org.iforce2d.myapp.MyActivity/shared/snapshot.jpg
我只使用Gmail,Facebook和Twitter作为接收应用程序进行了检查,但结果在各种操作系统版本(从2.2.1到4.4.3)中都非常一致,7个设备包含Kindle.
Gmail效果很好.图像缩略图显示在邮件撰写中,并在发送时成功附加到邮件.
Twitter和Facebook都崩溃了,如下所示.
以下是来自logcat的堆栈跟踪,显示了这两个应用程序所遇到的问题,对于它们来说似乎是同样的问题(这取自4.4.3,但错误实际上一直回到2.2. 1尽管错误消息的措辞略有不同):
Caused by:
java.lang.IllegalStateException: …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的Android项目中实现文件选择器.到目前为止我能做的是:
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
Run Code Online (Sandbox Code Playgroud)
然后在我的 onActivityResult()
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==-1){
Uri uri = data.getData();
String filePath = uri.getPath();
Toast.makeText(getActivity(), filePath,
Toast.LENGTH_LONG).show();
}
break;
}
Run Code Online (Sandbox Code Playgroud)
这是打开文件选择器,但它不是我想要的.例如,我想选择一个文件(.txt),然后获取它File然后使用它.有了这段代码,我想我会得到完整的路径,但它不会发生; 例如,我得到:/document/5318/.但是通过这条路径我无法获取文件.我创建了一个名为方法PathToFile()返回一个File:
private File PathToFile(String path) {
File tempFileToUpload;
tempFileToUpload = new File(path);
return tempFileToUpload;
}
Run Code Online (Sandbox Code Playgroud)
我正在试图做的是让用户选择File从任何地方表示DropBox,Drive,SDCard,Mega,等...我没有找到正确方式做到这一点,我试图让Path然后得到一个File本Path …
android android-intent android-file android-implicit-intent android-fileprovider
我一直在尝试按照Android教程共享文件.我这样设置FileProvider:
在主清单xml上:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mysecondapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
res/xml/filpaths.xml文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="myexternalimages" path="SpCars_album/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我尝试以下方法:
File requestFile = new File(mImageFilenames[position]);
try {
fileUri = FileProvider.getUriForFile(
SeventhActivity.this,
"com.example.mysecondapp.fileprovider",
requestFile);
} catch (IllegalArgumentException e) {
Log.e("File Selector",
"The selected file can't be shared: " +
mImageFilenames[position]);
}
Run Code Online (Sandbox Code Playgroud)
requestFile使用适当的文件工作路径进行实例化.该文件的路径完全以getExternalFilesDir(Environment.DIRECTORY_PICTURES)返回的内容开始.我只是无法理解是什么引起了错误,因为一切似乎都适合.提前致谢.
android filepath illegalargumentexception android-fileprovider
我有一个关于android FileProvider的问题.我想保存一个pdf文档并使用默认程序打开它.我不想将它保存在外部存储中.
在我成功将pdf保存到FilesDirectory/export/temp.pdf之后,
我尝试使用FileProvider.getUriForFile()生成URI.
File path = new File(getFilesDir(), "export");
File pdf = new File(path + File.separator + "temp.pdf");
pdf.getParentFile().mkdirs();
if (!pdf.exists())
pdf.createNewFile();
Uri uri = FileProvider.getUriForFile(getApplicationContext(), "?", pdf);
Run Code Online (Sandbox Code Playgroud)
问题:我必须传递什么作为第二个参数"权限" - 我的文件的位置,可以授予URI权限的类或其他什么?无论我尝试过什么导致IllegalArgumentException或NullPointerException.我的FileProvider(XML):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myApp.myActivity"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
引用文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="pdfTemplates" path="export/" />
</paths>
Run Code Online (Sandbox Code Playgroud) 我有两个申请.我正在尝试使用FileProvider将应用程序A中的文件共享到应用程序B. 应用程序A在应用程序B中的ContentProvider上调用insert方法以插入记录.插入的数据包括我想要从App A共享的文件的Uri.App B中的ContentProvider将尝试从App A读取共享文件.因为我没有使用Intent来共享文件,所以我是Context.grantUriPermission在App A中调用以允许读取(并且有时写入):
mContext.grantUriPermission(MyPackageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)
但是,执行此行会给我(名称已更改以保护无辜者):
java.lang.SecurityException: Uid 10066 does not have permission to uri content://au.com.example.AppA.fileprovider/MyFolder/MyFileName
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:2374)
at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1371)
at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:400)
at etc...
Run Code Online (Sandbox Code Playgroud)
应用程序A在清单文件中具有以下内容:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="au.com.example.AppA.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="au.com.example.READ_CONTENT"
android:writePermission="au.com.example.WRITE_CONTENT" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
filepaths.xml有:
<paths>
<files-path
name="MyFolder"
path="MyFolder/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
App A和App B都有以下内容:
<uses-permission android:name="au.com.example.READ_CONTENT" />
<uses-permission android:name="au.com.example.WRITE_CONTENT" />
Run Code Online (Sandbox Code Playgroud)
我已经尝试在两个应用中定义权限.它们都使用相同的调试签名进行签名:
<permission
android:name="au.com.example.READ_CONTENT"
android:permissionGroup="MyGroup"
android:protectionLevel="signature" >
</permission>
<permission
android:name="au.com.example.WRITE_CONTENT"
android:permissionGroup="MyGroup"
android:protectionLevel="signature" >
</permission>
Run Code Online (Sandbox Code Playgroud)
文件最终的实际路径是: …
我正在尝试为共享文件设置fileprovider.我的文件保存在外部存储器中的"AppName"文件夹中(与Android,Movies和Pictures文件夹相同).
这是我的文件提供程序配置:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.appname.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
和file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="mypath" path="AppName" />
</paths>
Run Code Online (Sandbox Code Playgroud)
当我尝试访问我的文件时:
Uri fileUri = FileProvider.getUriForFile(activity, "com.mydomain.appname.fileprovider",
new File("/storage/emulated/0/AppName/IMG_20160419_095211.jpg"));
Run Code Online (Sandbox Code Playgroud)
它返回一个错误:java.lang.IllegalArgumentException:无法在android.support.v4.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider.java:678)中找到包含/storage/emulated/0/AppName/IMG_20160419_095211.jpg的已配置根目录)在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
它在我使用像图片或电影这样的内置目录之前工作正常,我的file_paths.xml定义如下:
<external-path name="photos" path="Pictures" />
<external-path name="videos" path="Movies" />
Run Code Online (Sandbox Code Playgroud)
但现在我想将我的文件存储在我自己的文件夹中.我是否错过了使用FileProvider配置的东西?
我找到了很多与FileProvider相关的链接,但我找不到缓存目录的解决方案
java.lang.IllegalArgumentException:无法找到包含的已配置根目录
/data/data/pkg name/cache/1487876607264.png
我想将它用于CACHE DIRECTORY,我如何在提供者中提供路径.
<paths>
<external-path name="external_files" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)
我用它作为:
File file = new File(context.getCacheDir(), System.currentTimeMillis() + ".png");
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);
Run Code Online (Sandbox Code Playgroud)
如果我提供应用程序文件夹路径,但它不能使用缓存目录,它的工作正常.
任何帮助?
android caching android-fileprovider android-7.0-nougat android-7.1-nougat
我无法从"下载文件夹"中打开任何文件.
我可以下载一个文件并保存在下载文件夹中:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(descricao);
request.setTitle(titulo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nome);
enq = downloadManager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
在此之后,我的文件保存在目录文件夹中:Android >>内部共享存储>>下载.***我看到这条路径在ubuntu中手动打开设备的高清.由于图像显示路径. Android HD由ubuntu文件夹 - 查看路径
我尝试用这个打开这个文件:
downloadManager = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enq);
Cursor c = downloadManager.query(query);
if(c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if(DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = …Run Code Online (Sandbox Code Playgroud) directory android download android-download-manager android-fileprovider
我正在使用文件提供程序将照片保存到给定目的地.我明白了:
java.lang.IllegalArgumentException:尝试打开活动以从摄像头捕获图像时缺少android.support.FILE_PROVIDER_PATHS元数据.
我的manifest.xml文件:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
我的paths.xml文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="content" path="Android/data/com.my_package_name/files/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
和Java代码:
File externalFilesDirectory = this.getExternalFilesDir(null);
File imageFile = File.createTempFile(
imageFileName,
".jpg",
externalFilesDirectory
);
Uri photoURI = FileProvider.getUriForFile(this, "com.example", imageFile);
Run Code Online (Sandbox Code Playgroud)
最后一行给出了例外.我在这里错过了什么?我已经关注了官方Android开发网站的教程(https://developer.android.com/training/camera/photobasics.html)
android android-image android-xml android-file android-fileprovider
android ×9
android-file ×2
android-xml ×1
authority ×1
caching ×1
directory ×1
download ×1
filepath ×1
pdf ×1
permissions ×1