Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:15:26+00:00 2026-06-05T14:15:26+00:00

I am trying to retrieve a reference to a file stored in the assets

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:15:27+00:00Added an answer on June 5, 2026 at 2:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use ServletContext.getResource to retrieve a java.net.url reference to an image file
I am trying to retrieve values from my App.config file which is stored in
Hi I am trying to retrieve values from database. I have a row which
I'm trying to retrieve a stored date in a MS Access 2003 database ..
i'm trying to retrieve a value from my preference file which is an integer.
I'm trying to retrieve a latlng of a place using a reference. For some
All, Cannot I have created a Word2007 Document Project and I'm trying to retrieve
I'm trying to retrieve values from an XML file and I find some values
I'm trying to retrieve a user object from a foreign key reference but each
I am trying to retrieve an IOleCommandTarget reference from a panel control handle, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.