调用从Android的工作区文件夹中的文件路径 [英] Calling a file path from the android workspace folder

查看:185
本文介绍了调用从Android的工作区文件夹中的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有权点击我的项目名称,并成功创建了一个名为PDF文件的文件夹。我想pre负载在这里某些PDF文件,所以我将如何把这个路径/ somepdffile.pdf从我mainactivity类;

Basically I have right clicked on my project name and successfully created a new folder called pdfs. I want to pre load some pdf files in here so how would I call this path/somepdffile.pdf from my mainactivity class;

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

 }

}

推荐答案

您的新文件夹会被建筑商被完全忽略。它不会被打包您的应用程序,并复制到任何模拟器或设备。如果要打包与您的应用程序的数据,您需要使用原始或资产的文件夹。后者允许子文件夹,这样您就可以将您的PDF文件夹存在。

Your new folder will be completely ignored by the builders. It will not be packaged with your app and copied onto any emulator or device. If you want to package data with your app, you need to use the raw or assets folders. The latter allows subfolders, so you could move your pdfs folder there.

下面是一些code,它会在你的应用程序的移动文件从资产/ PDF文件文件/ PDF文件指定外部目录。

Here's some code that will move files from assets/pdfs to files/pdfs under your app's designated external directory.

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

这篇关于调用从Android的工作区文件夹中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆