Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

java - Android - Access file from assets PDF display

I am trying to retrieve a reference to a file stored in the assets directory to a file named myfile.pdf. I have tried to do it as follows:

File file = new File("android_assest/myfile.pdf);
Log.d("myTag", "" + file.isFile());

Somehow, I get false when the myfile.pdf do exists in the assets directory. I verified it using getAssets().list("") and Log.d() each element in the returned array.

More of which, I am trying to get a reference to a PDF file and then use any PDF viewer, which is already installed on the device, in order to view the PDF.

I guess that since the previous issue (retrieving a reference to the file) returns false then the next snipped code fails:

Intent i = new Intent(Intent.ACTION_VIEW,
    Uri.parse("file:///android_asset/myfile.pdf"));
startActivity(i);

Anyone has a clue why I am unable to retrieve a reference to the file? and why I cannot use already installed PDF viewer to display a PDF (after retrieving a reference to the PDF file)?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Barak said you can copy it out of assets to internal storage or the SD card and open it from there using inbuilt pdf applications.

Following Snippet will help you. (I have updated this code to write to and read files from internal storage.

But i dont recommend this approach because pdf file can be more than 100mb in size.

So its not recommended to save that huge file into internal storage

Also make sure while saving file to internal storage you use

openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

Then only other applications can read it.

Check following snippet.

package org.sample;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

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);
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }

}

Make sure to include

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in manifest


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...