为什么打开图像时Android Open File Intent会挂起? [英] Why would an Android Open File intent hang while opening an image?

查看:85
本文介绍了为什么打开图像时Android Open File Intent会挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:当 openFile()意向尝试 StartActivityForResult 时,应用程序挂起并出现空白屏幕和圆形光标.文件已存储到应用程序.

我已完成的工作:在此问题之前和之后,我研究了如何使用Intent打开文件.我发现了许多类似但不同的方法,并使用文字示例或各种示例的组合来查看是否可以找到并解决该问题(请参阅下文).我没有收到发现的任何类型的错误消息,并且以前有 FileUriExposedException ,但已解决.这些文件存储在应用程序中,可能是权限问题,并尝试了我所知的知识,包括使用外部读写"更新清单,并在访问意图上添加了标志设置.

我要执行的操作:我正在尝试学习如何通过具有简单JPG图像的意图打开文件,然后在我理解如何打开后最终扩展为打开各种文件类型

下面是我正在使用的当前代码,它包含一个MimeTypeMap,我尝试使用它代替"image/jpeg",以防万一我的语法在MIME类型上不正确,这似乎没有影响.

  private void openFile(Uri uri,String fName){MimeTypeMap myMime = MimeTypeMap.getSingleton();字符串mimeType = myMime.getMimeTypeFromExtension(getUpperBound(fName));Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.parse("content://" + uri),"image/jpeg");intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);startActivityForResult(intent,2);} 

所产生的问题的图像在以下位置悬而未决:

我尝试过的某些引用链接:

https://developer.android.com/guide/components/intents-common

ACTION_VIEW意图用于具有未知MimeType的文件

打开包含意图的文件(Android)

http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html

解决方案

我能够解决问题,并结合S-Sh关于考虑使用 FileProvider 的第二个建议.我必须进行一些改进,最后完成以下工作.我还提供了我使用过的资源的链接.

解决方案的上下文:

作为以后的读者对我的Android应用程序中此解决方案的上下文的注意,通过单击 TableLayout 中的可点击" TableRow 启动以下代码.TableLayout将每个文件名和文件ID列出为TableRow.单击一行后,将获取所选TableRow的 onClick 方法文件名,并使用 File 类和 FileProvider 并将文件名传递给创建一个 Uri .然后将此Uri传递给我创建的 openFile(Uri uri)方法,该方法封装了(用来说)用于打开文件的 Intent .

代码

将文件提供程序添加到`标签内的 AndroidManifest.xml"中:

 <代码><提供者android:name ="androidx.core.content.FileProvider"android:authorities ="com.mistywillow.fileprovider"android:exported ="false"android:grantUriPermissions ="true"><元数据android:name ="android.support.FILE_PROVIDER_PATHS"android:resource ="@ xml/file_paths"/></provider> 

res 目录中创建 file_paths.xml xml 目录(res/xml/):

 <?xml version ="1.0" encoding ="utf-8"?>< paths xmlns:android ="http://schemas.android.com/apk/res/android">< files-path name ="myFiles" path =."/></paths> 

onClick 捕获文件名并准备将Uri传递给openFile():

  row.setOnClickListener(v-> {TableRow tablerow =(TableRow)v;TextView示例=(TextView)tablerow.getChildAt(1);字符串结果= sample.getText().toString();文件filePaths = new File(getFilesDir().toString());File newFile =新文件(filePaths,结果);Uri contentUri = getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",newFile);openFile(contentUri);}); 

包含打开文件意图的 openFile(Uri uri)方法:

  private void openFile(Uri uri){Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(uri);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);startActivityForResult(intent,2);} 

引用的链接:

https://developer.android.com/reference/androidx/core/content/FileProvider

FileProvider-IllegalArgumentException:无法找到配置的根

My Problem: When openFile() intent attempts StartActivityForResult, the application hangs with a blank screen and circular cursor. The file(s) are stored to app.

What I have done: Before and after this issue I researched how to open files by use of Intents. I found a number of similar but different approaches and either used the literal example or a combination of various examples to see if I could find and resolve the issue (see some below). I'm not receiving any type of error message that I have found, and I had the FileUriExposedException previously but resolved it. These files are stored to the app and it may be a permissions issue and have tried what I know, including updating the manifest with for External Read and Write and added a flag setting on the intent for access.

What I'm trying to do: I'm trying to learn how to open files through intents with simple JPG images, then eventually expand to opening various file types after I understand how to do so.

Below is the current code I'm using and it included a MimeTypeMap that I tried in place of "image/jpeg" in case my syntax was not correct on the MIME Type, which did not seem to have an impact.

private void openFile(Uri uri, String fName) {

        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        String mimeType = myMime.getMimeTypeFromExtension(getUpperBound(fName));

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("content://" + uri), "image/jpeg");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, 2);
    }

An image of the resulting issue hanging and never opening below:

Some of the referenced links I tried:

https://developer.android.com/guide/components/intents-common

ACTION_VIEW intent for a file with unknown MimeType

Open File With Intent (Android)

http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html

解决方案

I was able to resolve my issue combined with S-Sh's second suggestion of considering the use of FileProvider. I had to perform some refinements and ended up with the following to work. I also provided links to the sources in which I used.

Context of the solution:

As a note to future readers as to the context of this solution within my Android app, the code below is launched by clicking a "clickable" TableRow in a TableLayout. The TableLayout lists each filename and file ID as a TableRow. Once a row is clicked, the onClick method of the selected TableRow the filename is acquired and the File class and FileProvider are used and filename passed to create an Uri. This Uri is then passed to an openFile(Uri uri) method I created encapsulating (so-to-speak) the Intent used to open the file.

Code

Adding of the FileProvider to the AndroidManifest.xml' within the` tag:

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.mistywillow.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
    <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

Creating within res directory the file_paths.xml and xml directory (res/xml/):

<?xml version="1.0" encoding="utf-8"?>
   <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <files-path name="myFiles" path="." />
   </paths>

The onClick capturing the filename and preparing the Uri to be passed to the openFile():

    row.setOnClickListener(v -> {

        TableRow tablerow = (TableRow) v;
        TextView sample = (TextView) tablerow.getChildAt(1);
        String result = sample.getText().toString();

        File filePaths = new File(getFilesDir().toString());
        File newFile = new File(filePaths, result);
        Uri contentUri = getUriForFile(getApplicationContext(), "com.mydomain.fileprovider", newFile);
        openFile(contentUri);

    });

The openFile(Uri uri) method containing the Intent to open a file:

private void openFile(Uri uri){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, 2);
}

Links referenced:

https://developer.android.com/reference/androidx/core/content/FileProvider

FileProvider - IllegalArgumentException: Failed to find configured root

这篇关于为什么打开图像时Android Open File Intent会挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆