2015-12-07 5 views
1

Im используя следующий метод для копирования PDF-файла из папки с данными во внутреннюю память в android. Я намерен открыть его с помощью MUPDF Reader. Поскольку он не поддерживает прямое открытие с что они делают это. Но, похоже, нет ответов на SO или где-либо, чтобы получить внутреннее хранилище в Android. Мне просто нужно открыть скопированный файл «Sample.pdf» из внутреннего хранилища. Пожалуйста, помогите.Получить внутренний путь хранения, чтобы открыть Скопированный PDF, используя MUPDF

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

Я попробовал метод из вашего answer.I получить следующие

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 
+0

Вы просто хотите скопировать файл Sample.pdf из папки справки во внутреннее хранилище? а затем захотелось открыть в MUPDF? – dex

+0

@dex Yeah.As mupdf не может открыть из активов напрямую, я полагаю. – techno

+0

@dex copyPath возвращается null – techno

ответ

2

Я решил эту проблему следующим контексте метод Context = getAppContext(); String copyPath = copyFileFromAssetsFolderToStorage (контекст, «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); 
    } 
} 
+0

Спасибо .. Но какой метод я должен использовать? Каковы параметры для метода copyFileFromAssetsFolderToStorage? – techno

+0

user copyFileFromAssetsFolderToStorage метод для копирования файла из папки справки во внутреннее хранилище Android. Добавление Javadocs для получения дополнительных пояснений – dex

+0

Спасибо, но у меня есть Uri для MUPDF как Uri uri = Uri.parse (путь + "// Sample.pdf"); Как это изменить. – techno

Смежные вопросы