我使用以下方法将PDF文件从资产文件夹复制到android.I中的内部内存意图使用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
答案 0 :(得分:2)
我通过以下方法解决了这个问题 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);
}
}