相关疑难解决方法(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应用程序中阅读pdf?

我正在申请要求打开pdf.

我在资产文件夹中也有一些pdf,所以我无法直接在webview中打开它.

默认情况下,android不支持pdf.

有没有适用于android的API(MuPdf除外)?

我的设备没有安装任何pdf阅读器,因此ACTION VIEW对我没用

以下不工作.......

在Android上使用Java渲染PDF文件

在应用程序中打开资产文件pdf

你可以建议我任何好的api ...

提前致谢...

android pdf-reader

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

如何在没有外部应用程序的情况下打开/显示文档(.pdf,.doc)?

我想创建一个程序,打开没有外部应用程序的文档.我需要这个,因为我想用手机方向滚动文件(Pitch and Roll).我在屏幕底部创建了一个按钮,当我按住按钮时,我也可以滚动文档.如果我松开按钮,我无法滚动它.因此,如果我使用外部应用程序打开文档,我的按钮会消失,而sensorManager也不会工作.

有人有任何想法来解决这个问题.或者有任何想法,如何滚动文档,在外部应用程序中打开,我的手机方向?

(对不起我的英语不好)

这是我的表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orientationscrolling"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.orientationscrolling.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical" >

<Button 
   android:id="@+id/mybutt"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="25sp"
   android:text="Scroll!!"
   android:layout_gravity="right"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); …
Run Code Online (Sandbox Code Playgroud)

pdf android external

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

如何从assets文件夹中打开Android中的PDF文件?

有没有人知道如何在Android中打开PDF文件?我的代码看起来像这样:

public class SampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadAssets();    
    }

    private void CopyReadAssets() {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "git.pdf");
        try {
            in = assetManager.open("git.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/git.pdf"),
                "application/pdf");

        startActivity(intent); …
Run Code Online (Sandbox Code Playgroud)

pdf android

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

如何从资产文件夹创建文件对象?

我正在尝试从资产文件夹中读取pdf文件,但我不知道如何获取pdf文件的路径。我右键单击pdf文件,然后选择“复制路径”并粘贴在此处输入图片说明

这是我的代码的另一个屏幕截图: 在此处输入图片说明

这是我的代码:

File file = new File("/Users/zulqarnainmustafa/Desktop/ReadPdfFile/app/src/main/assets/Introduction.pdf");

    if (file.exists()){
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            this.startActivity(intent);
        }
        catch (ActivityNotFoundException e) {
            Toast.makeText(this, "No application available to view PDF", Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(this, "File path not found", Toast.LENGTH_LONG).show();
    }
Run Code Online (Sandbox Code Playgroud)

我总是找不到文件,请帮助我创建File对象,或者让我知道如何获取我也尝试过file:///android_asset/Introduction.pdf但没有成功的文件的确切路径。我也尝试使用Image.png,但从未成功过file.exists()。我正在使用Mac版本的Android Studio。谢谢

java pdf android android-studio

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

Android - 阅读pdf并使用Intent显示它

我正在开发一个应用程序,因为我想从资产中显示一个pdf文件.我做了很多谷歌,并尝试了多种排列和组合,但没有工作.

码:

private void CopyReadAssets()
{
    AssetManager assetManager = getActivity().getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.pdf");
    try
    {
        in = assetManager.open("abc.pdf");
        out = getActivity().openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    }
    catch (Exception e)
    {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = …
Run Code Online (Sandbox Code Playgroud)

pdf android android-intent

0
推荐指数
1
解决办法
1356
查看次数