2

Im using the following method to copy a PDF File from the assets folder to internal memory in android.I intent to open it using MUPDF Reader.As it does not support direct opening from assets folder im doing this.But there seems to be no answers on SO or anywhere to get the Internal Storage location in Android.I just need to open the copied file 'Sample.pdf' from the internal storage.Please help.

private void copyAssets() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("");
        } catch (IOException e) {
            Log.e("tag", "Failed to get asset file list.", e);
        }
        if (files != null) for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open(filename);
              File outFile = new File(getExternalFilesDir(null), filename);
              out = new FileOutputStream(outFile);
              copyFile(in, out);
            } catch(IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            }     
            finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // NOOP
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // NOOP
                    }
                }
            }  
        }
    }
    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);
        }
    }

I tried the method from your answer.I get the following

12-07 12:33:42.425: E/libmupdf(1858): Opening document...
12-07 12:33:42.427: E/libmupdf(1858): error: cannot open null//Sample.pdf
12-07 12:33:42.428: E/libmupdf(1858): error: cannot load document 'null//Sample.pdf'
12-07 12:33:42.429: E/libmupdf(1858): error: Cannot open document: 'null//Sample.pdf'
12-07 12:33:42.429: E/libmupdf(1858): Failed: Cannot open document: 'null//Sample.pdf'
12-07 12:33:42.433: I/System.out(1858): java.lang.Exception: Failed to open null//Sample.pdf
techno
  • 6,100
  • 16
  • 86
  • 192

1 Answers1

2

I solved this problem by following method Context context = getAppContext(); String copyPath = copyFileFromAssetsFolderToStorage(context, "Sample.pdf", "Sample.pdf",context.getExternalFilesDir(null).getAbsolutePath());

Now once you get the copyPath , now you can use content provider to share this file with mupdf reader app.

if (copyPath != null)
{
    File fileToOpen = new File (copyPath);
    Uri uri = Uri.fromFile(fileToOpen);
}

/**
 * Saves a file from assest folder to a path specified on disk (cache or sdcard).
 * @param context
 * @param assestFilePath    : path from assestfolder for which input stream need to called e.g fonts/AdobeSansF2-Regular.otf
 * @param fileName          : file name of that assest
 * @param filePathInStorage : specified path in the storage
 * @return Absolute path of the file after saving it.
 */
public static String copyFileFromAssetsFolderToStorage(Context context, String assestFilePath, String fileName, String filePathInStorage) 
{
    AssetManager assetManager = context.getAssets();
    InputStream in = null;
    OutputStream out = null;
    File copyFileDir = null;
    try 
    {
        in = assetManager.open(assestFilePath);
        copyFileDir = new File(filePathInStorage, fileName);
        out = new FileOutputStream(copyFileDir);
        copyFile(in, out);
    } 
    catch(IOException e) 
    {
    }     
    finally
    {
        if (in != null)
        {
            try 
            {
                in.close();
            }
            catch (IOException e)
            {
            }
        }
        if (out != null) 
        {
            try 
            {
                out.close();
            }
            catch (IOException e) 
            {
            }
        }
    }
    return copyFileDir != null ? copyFileDir.getAbsolutePath() : null;
}
private static 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);
    }
}
dex
  • 5,182
  • 1
  • 23
  • 41
  • Thanks..But which method should i use?What are the parameters for copyFileFromAssetsFolderToStorage method? – techno Dec 07 '15 at 06:13
  • user copyFileFromAssetsFolderToStorage method to copy file from assest folder to Internal Storage location of Android. Adding Javadocs for more clarification – dex Dec 07 '15 at 06:14
  • Thanks but i have the Uri for MUPDF as Uri uri = Uri.parse(path+"//Sample.pdf"); How to modify this. – techno Dec 07 '15 at 06:18
  • You get the uri as below : File fileToOpen = new File (copyPath); Uri uri = Uri.fromFile(fileToOpen); – dex Dec 07 '15 at 06:21
  • Thanks.But where do i get the copyPath.Im confused. – techno Dec 07 '15 at 06:42
  • when you call copyFileFromAssetsFolderToStorage() method it will return you a copyPath which is basically path where in Android Internal Storage where your "Sample.pdf" file get copied. – dex Dec 07 '15 at 06:45
  • @techno can you please share your code of calling copyFileFromAssetsFolderToStorage () , and also confirm that Sample.pdf is present inside assest folder or not ? – dex Dec 07 '15 at 07:48
  • I solved it.There was no pdf file in assets folder.Thanks for your help :) – techno Dec 07 '15 at 08:22
  • Did you find a way to navigate to a page without starting a new activity and using without intent. – techno Dec 15 '15 at 04:51
  • @techno : view pager is a option I guess. – dex Dec 15 '15 at 05:14
  • I solved it using startActivityForResult(intent, 0).Its very easy.They are using the same thing,a whole lot of code in OutlineActivity was confusing. – techno Dec 15 '15 at 05:19
  • MuPDF takes too much time to open file. Is there any way to show loader until it loads completely ? – Anand Savjani Jun 22 '16 at 18:14