在我的应用程序中,我有自定义自动下载和安装APK它的工作原理如下
// auto register for the complete download
activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
// Download the file through DownloadManager
String destination = Environment.getExternalStorageDirectory() + "/";
String fileName = "myfile.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setDescription("description");
request.setTitle("title");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
// BEFORE working doing this
//install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//install.setDataAndType(uri, …Run Code Online (Sandbox Code Playgroud) 我正在我的应用程序上创建一个PDF文件,并将其写入外部存储"下载"目录.现在,当我通过我的应用程序通过intent action.VIEW使用FileProvider uri打开它时,Google PDF Viewer显示一个空白屏幕,Adobe Acrobat甚至无法打开该文件.这是我编写文件然后尝试显示它的代码.请注意,我正在发送本地通知并尝试通过通知打开文件.
try {
File mypath=new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
document.writeTo(new FileOutputStream(mypath));
document.close();
NotificationManager notificationManager = (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);
File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
Uri pdfUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.onur.emlakdosyasi.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//use the flag FLAG_UPDATE_CURRENT to override any notification already there
PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getContext())
.setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.pdficon)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
notificationManager.notify(10, notification);
} catch (FileNotFoundException e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) pdf android android-intent android-pendingintent android-fileprovider
是否可以将FileProvider用于其他应用程序?
的manifest.xml
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:exported="true"
android:grantUriPermissions="false"
android:permission="com.example.filesvisible.permission.READ" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filespath" />
</provider>
...
Run Code Online (Sandbox Code Playgroud)
来自doc:
false:提供程序不可用于其他应用程序.设置android:exported ="false"以限制对应用程序的提供程序的访问.只有与提供者具有相同用户ID(UID)的应用程序才能访问它.
我试图将导出设置为true但我得到了这个异常
Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must not be exported为什么我无法导出FileProvider?
provider android android-contentresolver android-fileprovider
我正在将文档访问框架与我的云存储访问应用程序集成.使用此文档,我可以访问该文件并检索它(我只是使用Gmail应用程序的附加功能来检查).
我现在正试图找到如何使用相同的方法保存文件(通过应用程序直接将文件保存到云存储),我做了以下更改:
对于getRoots调用,
row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE);
Run Code Online (Sandbox Code Playgroud)
我也覆盖了这个createDocument方法.
我没有看到有关如何执行此操作的示例代码或文档的方法.我也看到像"照片"应用程序这样的应用程序有"共享"按钮使用不同的方法(日志显示miniShareActivity),我的应用程序没有显示在那里(看起来它使用不同的文件共享机制)
我正在寻找有关的信息
如何使用SAF存储文件(任何样本文件都很棒或指向文档).我假设它将允许用户使用选择器界面导航到文件夹并存储文件.
如何让应用程序显示在"Minishare活动"应用程序列表中以将文件导入应用程序(看起来它不提供选择器界面但我仍然希望提供支持以便将文件保存到默认位置)
我创建了一个可以在其内部存储中导入文件的应用程序.为了打开与外部应用程序文件(例如PF观众或照片),我试图按照这些指南:官方指南,TOPIC1,标题2,topic3和topic4但没有成功.
这是我的代码:
在我的清单中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.chatcher"
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)
我的包价值: package="com.myapp.catcher"
我的file_paths.xml
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="projection" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)
我的代码
String fileName = path.substring(path.lastIndexOf("/") + 1);
String shelf = path.substring(path.lastIndexOf("PRIVATE") + 8, path.lastIndexOf("/"));
File filePath = new File(mContext.getFilesDir(), "PRIVATE".concat("/").concat(shelf).concat("/"));
File newFile = new File(filePath, fileName);
Uri contentUri = FileProvider.getUriForFile(mContext, "com.myapp.chatcher", newFile);
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(contentUri);
myIntent.setType(mimeType);
myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
mContext.startActivity(myIntent);
Run Code Online (Sandbox Code Playgroud)
我创建了这样的层次结构:
PRIVATE -> …Run Code Online (Sandbox Code Playgroud) 随着 android N 最近的变化,我不得不升级我的代码以使用 FileProvider 使用相机/文件管理器获取图像/文件。该代码在模拟器(genymotion)中运行良好,但在 Moto G4 plus 中抛出 IO 异常。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
try {
ParcelFileDescriptor fcd = getContentResolver().openFileDescriptor(uriOrig,"r");// uriOrig is the uri returned from camera
if(fcd != null) {
InputStream inputStream = new FileInputStream(fcd.getFileDescriptor());
uploadCall =RestService.getInstance().uploadFile(mimeType, name, size,
inputStream, defaultResponseCallback);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这里是上传文件的方法。
public Call<Attachment> uploadFile(final String mimeType, final String name, final long length,final InputStream is, final Callback<Attachment> callback) {
final int byteCount = 8192;
if (length > 0) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写我的第一个Android应用程序,这将涉及拍照并用它做事.
我在网上查看了几个教程之后编写了一些代码,但是只要点击按钮就会得到以下NullPointerException:
10-03 14:48:00.284 26310-26310/org.broadinstitute.jsnap E/MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
10-03 14:48:00.293 26310-26310/org.broadinstitute.jsnap E/MYAPP: exception
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at org.broadinstitute.jsnap.MainActivity.takePhoto(MainActivity.java:54)
at org.broadinstitute.jsnap.MainActivity.access$000(MainActivity.java:24)
at org.broadinstitute.jsnap.MainActivity$1.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Run Code Online (Sandbox Code Playgroud)
我不太清楚如何解决.这是我的相关代码:
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.broadinstitute.jsnap"> …Run Code Online (Sandbox Code Playgroud) java camera android nullpointerexception android-fileprovider
我正在尝试使用 Gmail 通过电子邮件发送 PDF 文件。但是,Gmail 应用程序显示吐司:
无法附加文件
PDF 文件未损坏,并在应用程序的缓存目录中成功生成。
代码:(如果您需要 Java 代码,请在下面评论。):
val photoURI: Uri = FileProvider.getUriForFile(this, "com.packagename.provider",
File(this.cacheDir.path + "/Report.pdf"))
val emailIntent = Intent(Intent.ACTION_SENDTO)
emailIntent.data = Uri.parse("mailto:")
emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("some@domain.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
startActivity(emailIntent)
Run Code Online (Sandbox Code Playgroud)
请帮忙
android file-sharing android-file kotlin android-fileprovider
我有一个临时文件,用于从裁剪库创建图像,我可以在设备文件资源管理器中看到该文件,但当我尝试打开该文件时,出现此错误:
java.io.FileNotFoundException:文件:/data/user/0/com.example.demo/cache/.tmp/cropped1651879842159823361.png:打开失败:ENOENT(没有这样的文件或目录)
该文件的创建方式如下:
val croppedImageFile = File.createTempFile("cropped", ".png", viewModel.tempPath)
val destinationUri = Uri.fromFile(croppedImageFile)
Run Code Online (Sandbox Code Playgroud)
viewModel.tempPath 如下:
viewModel.tempPath = "${this.cacheDir}/.tmp"
Run Code Online (Sandbox Code Playgroud)
我可以看到该文件已创建并且有效,但是当我尝试访问它时,它声称它不存在。我只是通过执行以下操作打开文件File(uri.toString())。在视图模型中
我不确定出了什么问题以及为什么找不到该文件。如果这很重要,我使用的是带有 google play 的模拟器,并且是 Android 11。
根据FileProvider 文档,该fileProvider.openFile()方法返回
一个新的ParcelFileDescriptor,您可以使用它访问该文件。
我查看了ParcelFileDescriptor,但我看不到如何访问该文件。那么如何从 ParcelFileDescriptor 访问文件呢?