试图读取存储在res文件夹android中的pdf文件

时间:2014-03-24 10:12:55

标签: android pdf runtime-error

以下是下面给出的代码。每当我尝试打开dat活动时,它显示目标文件不存在..请帮助我.....提前感谢

public class MainActivityAlgb extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_activity_algb);
            CopyReadAssets();

        }

        private void CopyReadAssets()
        {
            AssetManager assetManager = getAssets();

            InputStream in = null;
            OutputStream out = null;
            File file = new File(getFilesDir(), "cure.pdf");
            try
            {
                in = assetManager.open("cure.pdf");
                out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch (Exception e)
            {
                Log.e("tag", e.getMessage());
            }

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/cure.pdf"), "application/pdf");

            startActivity(intent);
        }

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

    }

1 个答案:

答案 0 :(得分:0)

根据我的理解,您已将文件存储在res文件夹中。我假设它位于原始文件夹中,如果不将其移动到原始文件夹。然后您可以将文件输入流作为

InputStream is = getResources().openRawResource(R.raw.yourFile);

希望这有帮助。