相关疑难解决方法(0)

android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外

当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?

示例代码:

File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)

日志:

android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外

编辑:

在定位Android Nougat时,file://不再允许使用URI.我们应该使用content://URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?

android android-file android-7.0-nougat

682
推荐指数
15
解决办法
38万
查看次数

以编程方式从Android的内置Gallery应用程序中获取/选择图像

我正试图在我的应用程序内打开Gallery内置应用程序中的图像/图片.

我有一张图片的URI(图片位于SD卡上).

你有什么建议吗?

android image gallery android-intent

265
推荐指数
12
解决办法
30万
查看次数

从Android Intent打开图库应用程序

我正在寻找一种Android从意图中打开图库应用程序的方法.

我不想返回图片,而只是打开图库以允许用户使用它,就好像他们从启动器(View pictures/folders)中选择它一样.

我试图做以下事情:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);  
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

然而,一旦我选择了一张图片,这会导致应用程序关闭(我知道这是因为ACTION_GET_CONTENT),但我需要打开图库.

任何帮助都会很棒.

谢谢

java android android-intent

22
推荐指数
5
解决办法
8万
查看次数

从我的Android应用程序以编程方式打开所选文件(图像,pdf,...)?

我正在开发一个Android应用程序,该应用程序应该能够从特定文件夹中打开所选文件.

我已经尝试了这个,但在选择了我要打开它的应用程序后,我得到了这样的消息:

不可能加载

在尝试了很多线程1线程2之后,我使用这些代码行来完成它:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("/mnt/sdcard/xxx/xxx/Pictures/xxx.jpg"), "image/*");
myContext.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

我怎么能搞清楚这一点?

pdf android image android-intent

21
推荐指数
2
解决办法
8万
查看次数

如何在android中的库中打开位图

嗨,我想在图库中打开图片,下面是我的代码

mImageView.setImageBitmap(AppUtil.getBitmapFromFile(obj.getImageUrl(), 200, 200));

    mImageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(obj.getImageUrl()), "image/*");
            startActivity(intent);
        }
    });
Run Code Online (Sandbox Code Playgroud)

但它显示NullPointerException

Uri.parse(obj.getImageUrl() returns below string
Run Code Online (Sandbox Code Playgroud)

/mnt/sdcard/Pictures/app_images/pro20130429_170323_-1793725321.tmp

更新:现在我尝试
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("file://sdcard/Pictures/app_images/pro20130429_170323_-1793725321.tmp"))); 并收到错误

05-03 16:40:18.460: E/AndroidRuntime(4764): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file://sdcard/Pictures/app_images/pro20130429_170323_-1793725321.tmp }
Run Code Online (Sandbox Code Playgroud)

android photo-gallery android-intent

6
推荐指数
1
解决办法
6549
查看次数

如何为所有Android版本打开APK文件

背景

到目前为止,使用此意图有一种简单的方法来安装APK文件:

    final Intent intent=new Intent(Intent.ACTION_VIEW)
            .setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
Run Code Online (Sandbox Code Playgroud)

但是,如果你的应用程序中的Android API 24以上(牛轧糖- 7.0),你就可以或更新运行这段代码,你会得到一个异常,如图所示这里,例如:

android.os.FileUriExposedException: file:///storage/emulated/0/sample.apk exposed beyond app through Intent.getData()
Run Code Online (Sandbox Code Playgroud)

问题

所以我做了我被告知的事情:使用支持库的FileProvider类,如下:

    final Intent intent = new Intent(Intent.ACTION_VIEW)//
            .setDataAndType(android.support.v4.content.FileProvider.getUriForFile(context, 
            context.getPackageName() + ".provider", apkFile),
            "application/vnd.android.package-archive").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)

清单:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
Run Code Online (Sandbox Code Playgroud)

res/xml/provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<external-path name="external_files" path="."/>-->
    <external-path
        name="files_root"
        path="Android/data/${applicationId}"/>
    <external-path
        name="external_storage_root"
        path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)

但是,现在它仅适用于Android Nougat.在Android 5.0上,它抛出一个异常:ActivityNotFoundException.

我试过的

我可以添加一个Android OS版本的检查,并使用任何一种方法,但正如我所读,应该使用一种方法:FileProvider.

所以,我尝试使用我自己的ContentProvider充当FileProvider,但我得到了与支持库的FileProvider相同的异常.

这是我的代码:

    final Intent intent = new Intent(Intent.ACTION_VIEW)
        .setDataAndType(OpenFileProvider.prepareSingleFileProviderFile(apkFilePath),
      "application/vnd.android.package-archive")
      .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)

OpenFileProvider.java …

android android-intent apk android-fileprovider android-7.0-nougat

5
推荐指数
1
解决办法
2278
查看次数

特定文件夹中的内置图库

可能重复:
带文件夹过滤器的图库

我正在使用以下代码在我的应用程序内打开一个库

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, TEST_RESULT);
Run Code Online (Sandbox Code Playgroud)

我需要使用内置图库应用程序来显示特定文件夹中的图像/视频列表. - 当图库打开时,有一些Intent动作显示所选图像??? - 有没有办法确定特定的图像文件夹?**我希望图库只显示我文件夹中的图像/视频(而不显示其他系统文件夹).

android

3
推荐指数
1
解决办法
1万
查看次数