Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 994
EviatarS Avatar asked Jun 11 '12 13:06

EviatarS


People also ask

How do I access asset files on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

How do I open a PDF file from assets in WebView Android programmatically?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

How do I view PDF in Android WebView?

setJavaScriptEnabled(true); webView. loadUrl("https://docs.google.com/gview?embedded=true&url=" + "https://pdfurl.com/file.pdf"); That's all you can load a PDF file in WebView these simple steps.


2 Answers

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

like image 105
Vipul Avatar answered Nov 16 '22 01:11

Vipul


You can't do it like that. There is no directory structure in an apk, it is just data. The framework knows how to access it (getAssets), but you cannot look for it as a file in a directory.

You can open it as an input stream...

in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf))); 

Or you can copy it out of assets to internal storage or the SD card and access it there.

like image 25
Barak Avatar answered Nov 16 '22 00:11

Barak