使用 MUPDF 获取内部存储路径以打开复制的 PDF
Get Internal Storage Path to open Copied PDF using MUPDF
我使用以下方法将 PDF 文件从 assets 文件夹复制到内部存储器 android.I 打算使用 MUPDF 打开它 Reader.As 它不支持直接从 assets 文件夹打开我正在做this.But SO 或任何地方似乎都没有答案来获取 Android.I 中的内部存储位置,只需要从内部 storage.Please 帮助中打开复制的文件 'Sample.pdf'。
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
我用下面的方法解决了这个问题
上下文 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);
}
}
我使用以下方法将 PDF 文件从 assets 文件夹复制到内部存储器 android.I 打算使用 MUPDF 打开它 Reader.As 它不支持直接从 assets 文件夹打开我正在做this.But SO 或任何地方似乎都没有答案来获取 Android.I 中的内部存储位置,只需要从内部 storage.Please 帮助中打开复制的文件 'Sample.pdf'。
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
我用下面的方法解决了这个问题 上下文 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);
}
}