相关疑难解决方法(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中将文件写入assets文件夹或raw文件夹?

我正在开发一些应用程序,我必须从某个http位置更新assets/raw文件夹运行时中存在的一些文件.

任何人都可以通过共享如何在资产或原始文件夹中写入文件来帮助我吗?

android

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

java.io.FileNotFoundException:此文件无法作为文件描述符打开; 它可能是压缩的

我正在编写一个来自android的音板.问题是有些声音有效,有些无效.这是我得到的声音的回溯不起作用

05-31 13:23:04.227 18440 18603 W System.err: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
05-31 13:23:04.227 18440 18603 W System.err:    at android.content.res.AssetManager.openAssetFd(Native Method)
05-31 13:23:04.227 18440 18603 W System.err:    at android.content.res.AssetManager.openFd(AssetManager.java:331)
05-31 13:23:04.227 18440 18603 W System.err:    at com.phonegap.AudioPlayer.startPlaying(AudioPlayer.java:201)
05-31 13:23:04.227 18440 18603 W System.err:    at com.phonegap.AudioHandler.startPlayingAudio(AudioHandler.java:181)
05-31 13:23:04.235 18440 18603 W System.err:    at com.phonegap.AudioHandler.execute(AudioHandler.java:64)
05-31 13:23:04.235 18440 18603 W System.err:    at com.phonegap.api.PluginManager$1.run(PluginManager.java:86)
05-31 13:23:04.235 18440 18603 W System.err:    at java.lang.Thread.run(Thread.java:1096)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

java android filenotfoundexception cordova

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

Android - 将资产复制到内部存储

美好的一天!

我刚开始为android开发.在我的应用程序中,我需要将assets资源文件夹中的项目复制到内部存储.

我在SO上搜索了很多,包括将其复制到外部存储器. 如何将文件从'assets'文件夹复制到SD卡?

这就是我想要实现的:我有一个目录已存在于内部存储中,如X> Y> Z. 我需要一个文件复制到Y,另一个文件复制到Z.

任何人都可以帮我解决代码片段吗?我真的不知道如何继续这个.

对不起,我的英语不好.

非常感谢.

java android

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

Android:如何从资产文件创建File对象?

我在assets文件夹中有一个文本文件,我需要将其转换为File对象(而不是InputStream).当我尝试这个时,我得到"没有这样的文件"例外:

String path = "file:///android_asset/datafile.txt";
URL url = new URL(path);
File file = new File(url.toURI());  // Get exception here
Run Code Online (Sandbox Code Playgroud)

我可以修改它以使其工作吗?

顺便说一下,我尝试"按示例编写代码",查看我项目中其他位置引用资源文件夹中的HTML文件的代码片段

public static Dialog doDialog(final Context context) {
WebView wv = new WebView(context);      
wv.loadUrl("file:///android_asset/help/index.html");
Run Code Online (Sandbox Code Playgroud)

我承认我并不完全理解上述机制,因此我可能无法正常工作.

谢谢!

android assets file

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

使用Asset文件夹中的附件发送电子邮件

    //EMAIL SENDING CODE  FROM ASSET FOLDER
    email = editTextEmail.getText().toString();
    subject = editTextSubject.getText().toString();
    message = editTextMessage.getText().toString();
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("file/html");
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.example.deepa.xmlparsing/file:///android_assets/Combination-1.html"));
    startActivity(Intent.createChooser(emailIntent, "Send email using"));
Run Code Online (Sandbox Code Playgroud)

最后,我从资产文件夹(Combination-1.html)获取文件.

它越来越好了

找不到运行时错误文件异常.

有没有其他方式发送文件附件?

email android email-attachments

17
推荐指数
1
解决办法
1608
查看次数

在React Native中查看PDF

任何人都可以提供在React Native中显示PDF的示例代码吗?iOS和Android.

这就是我尝试过的:

  render: function() {
    return <WebView
      source={require('./my.pdf')}
      />
  }
Run Code Online (Sandbox Code Playgroud)

^这导致死亡的红色屏幕与"无法解决模块"错误.

  render: function() {
    return <WebView
      source={{uri: 'my.pdf'}}
      />
  }
Run Code Online (Sandbox Code Playgroud)

^这会在WebView中显示"Error Loading Page"消息."在此服务器上找不到请求的URL"

我知道iOS能够在UIWebView中显示PDF.我认为Android可以做同样的事情.必须存在如何指定我缺少的源.

pdf webview react-native

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

将目录从Assets复制到本地目录

我正在尝试使用我的资产文件夹中的目录并将其作为一个访问File.是否可以访问Assets目录中的某些内容File?如果没有,如何将目录从Assets文件夹复制到应用程序的本地目录?

我会像这样复制一个文件:

    try
    {
        InputStream stream = this.getAssets().open("myFile");
        OutputStream output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/myNewFile"));

        byte data[] = new byte[1024];
        int count;

        while((count = stream.read(data)) != -1)
        {
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        stream.close();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何为目录执行此操作.

我宁愿不围绕不起作用的东西构建我的基础设施,那么如何将目录从Assets复制到本地目录,或者是否可以访问我的Assets中的目录File

编辑

这就是我为自己的项目解决的问题:

InputStream stream = null;
OutputStream output = null;

for(String fileName : this.getAssets().list("demopass"))
{
    stream = this.getAssets().open("directoryName/" + fileName);
    output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/newDirectory/" + fileName));

    byte …
Run Code Online (Sandbox Code Playgroud)

android

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

Android设备上二进制文件损坏的可能原因是什么?

最近我正在调查二进制文件损坏的原因.具体来说,我们有一个Android应用程序,本机部分可以在SD卡上读/写二进制文件.有时,二进制文件因未知原因而破坏.我们从不同的用户那里收集了一些这些文件,并发现了一些有趣的事实.

一种大多数的损坏是,二进制文件的前4096字节被擦除.当我对这些文件进行十六进制时,前4096个字节都是零.不超过4096或小于4096,但正好是4096字节.我认为这不是巧合.我知道4096字节是一页大小.但是缺乏经验,我无法弄清楚原因,更重要的是,我不知道如何为其他用户/设备避免这样的事情.

除此之外,在一些二进制文件的中间,还有一些连续的零段,它不应该存在.如果这不是我们程序的错误,是否有任何可能的原因可能与平台/设备内核相关,或者其他任何设备突然断电?

我希望经历过类似情况的人能给我一些提示/建议/解决方案等等.这让我很困惑.

非常感谢〜

c c++ linux android binaryfiles

13
推荐指数
1
解决办法
8037
查看次数

将文件复制到assets文件夹

搜索1小时后,我找不到解决问题的方法.

我想将文件从sdcard移动到assets文件夹,并且还覆盖assets文件夹中的现有文件(这两个文件都是sqlite数据库,它们具有相同的名称,数据只有一点点差别)

??

java android inputstream file android-assets

9
推荐指数
1
解决办法
7797
查看次数