且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中的原始文件夹中打开文件

更新时间:2023-01-30 16:41:19

Unfortunately you can not create a File object directly from the raw folder. You need to copy it or in your sdcard or inside the application`s cache.

you can retrieve the InputStream for your file this way

    InputStream in = getResources().openRawResource(R.raw.yourfile);

  try {
       int count = 0;
       byte[] bytes = new byte[32768];
       StringBuilder builder = new StringBuilder();
       while ( (count = in.read(bytes,0, 32768)) > 0) {
           builder.append(new String(bytes, 0, count));
       }

       in.close();
       reqEntity.addPart(new FormBodyPart("file", new StringBody(builder.toString())));
   } catch (IOException e) {
       e.printStackTrace();
   }