我使用以下方法将PDF文件从资产文件夹复制到android的内部存储器中。我打算使用MUPDF Reader打开它。在SO上或在Android上获取内部存储位置的任何位置。我只需要从内部存储中打开复制的文件“ 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);
}
}
我从您的答案中尝试了该方法。我得到以下内容
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
最佳答案
我通过以下方法解决了这个问题
上下文上下文= getAppContext();
字符串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);
}
}