【问题标题】:Calling a file path from the android workspace folder从 android 工作区文件夹中调用文件路径
【发布时间】:2014-03-25 23:04:12
【问题描述】:

基本上我已经右键单击我的项目名称并成功创建了一个名为 pdfs 的新文件夹。我想在这里预加载一些 pdf 文件,那么如何从我的 mainactivity 类中调用这个 path/somepdffile.pdf;

import java.io.File;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FullscreenActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_fullscreen);

  Button button = (Button) findViewById(R.id.bPressMe);

  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

    File pdfFile = new File(Environment
      .getExternalStorageDirectory(), "pdfs/2pg.pdf");

    try {
     if (pdfFile.exists()) {
      Uri path = Uri.fromFile(pdfFile);
      Intent objIntent = new Intent(Intent.ACTION_VIEW);
      objIntent.setDataAndType(path, "application/pdf");
      objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(objIntent);
     } else {
      Toast.makeText(FullscreenActivity.this, "File NotFound",
        Toast.LENGTH_SHORT).show();
     }
    } catch (ActivityNotFoundException e) {
     Toast.makeText(FullscreenActivity.this,
       "No Viewer Application Found", Toast.LENGTH_SHORT)
       .show();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });

 }

}

【问题讨论】:

    标签: android path


    【解决方案1】:

    最好将它们放在 assets/ 文件夹中,这样您就可以使用 AssetManager 访问它们。像这样的

        AssetManager assetManager = getAssets();
    
        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "name_of_pdf_file.pdf");
        try
        {
            in = assetManager.open("name_of_pdf_file.pdf");
    
            in.close();
            in = null;
        } catch (Exception e)
        {
            Log.e("tag", e.getMessage());
        }
    

    【讨论】:

    • 你没有在你的方法中使用 File 文件,你只是在创建它?
    • @raklar 有了这个,您将获得 InputStream,因此您可以读取文件并对其内容做您想做的事情
    • @raklar 在您的情况下,您可以使用 OutputStream 将 PDF 文件写入 SDCard,然后按照您的意图传递文件路径
    【解决方案2】:

    如果您将 PDF 移动到 assets/,您可以使用 AssetManager 为它们检索 InputStream

    如果您的目标是在第三方 PDF 查看器中显示它们 - 正如您的代码所建议的那样 - 那么您可以:

    • 将 PDF 从资产复制到外部存储中,在这种情况下,您的其余代码将起作用,或者

    • 将 PDF 从资产复制到内部存储,然后使用 FileProvider 将其发布给 PDF 查看器,或者

    • 使用 my StreamProvider 直接从资产将 PDF 发布到查看器应用

    FileProvider 的覆盖范围可以在the Android documentation 中找到。我的 StreamProvider 是基于 Google 的 FileProvider 并且工作方式类似。

    【讨论】:

    • 可以在上面的类上实现吗?
    【解决方案3】:

    建设者将完全忽略您的新文件夹。它不会与您的应用程序打包并复制到任何模拟器或设备上。如果你想用你的应用打包数据,你需要使用 raw 或 assets 文件夹。后者允许子文件夹,因此您可以将 pdfs 文件夹移到那里。

    这里有一些代码可以将文件从assets/pdfs 移动到应用指定的外部目录下的files/pdfs

    final static String TAG = "MainActivity";
    final static int BUFF_SIZE = 2048;
    
    private void copyPdfs() {
        AssetManager assetManager = getAssets();
        String[] pdfs = null;
        try {
            pdfs = assetManager.list("pdfs");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Unable to get list of PDFs!");
        }
    
        File pdfDir = new File(getExternalFilesDir(null), "pdfs");
        if (!pdfDir.exists()) {
            Log.d(TAG, "Creating directory " + pdfDir.getAbsolutePath());
            pdfDir.mkdirs();
        }
    
        for (String pdf : pdfs) {
            Log.d(TAG, "Copying " + pdf);
            InputStream in = null;
            OutputStream out = null;
    
            try {
                in = assetManager.open("pdfs" + File.separator +  pdf);
                out = new FileOutputStream(new File(pdfDir, pdf));
    
                copyPdf(in, out);
    
                out.close();
                in.close();
    
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "Unable to copy PDF!");
            }
        }
    }
    
    private void copyPdf(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFF_SIZE];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
    
        out.flush();        
    }
    

    【讨论】:

    • 我如何从那里调用它?
    • 我用一些代码更新了我的答案。这些文件将被放入应用程序外部目录的子目录中。在我的设备上,它是“/storage/sdcard0/Android/data/”,但您应该始终使用 getExternalFilesDir() 进行访问。
    猜你喜欢
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2011-01-10
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多