小编use*_*534的帖子

如何获取drawable的File实例?

下面是我处理徽标打印的代码.徽标放在res/drawable文件夹中.当我运行应用程序时,它会抛出:

java.io.FileNotFoundException: /android.resource:/com.android.test/2130837505 (No such file or directory).
Run Code Online (Sandbox Code Playgroud)

有什么建议?

    public  boolean printLogo()
    {
      Uri logo_path = Uri.parse("android.resource://com.android.test/" + R.drawable._logo);
      File logo = new File(logo_path.toString());
      byte[] logo_bytes = new byte[(int) logo.length()];
      System.out.print("Length:" + logo.length());
      FileInputStream fs;
      try {
          fs = new FileInputStream(logo);
          fs.read(logo_bytes);
          fs.close();
          mChatService.write(logo_bytes);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }catch (IOException e) {
          e.printStackTrace();
      }
      return true;
    }
Run Code Online (Sandbox Code Playgroud)

android uri file fileinputstream drawable

9
推荐指数
1
解决办法
9095
查看次数

我在Android应用资产中读取和写入文件有什么问题?

代码优先:

AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets();

try {
    Log.e("Glenn:", address);
    FileOutputStream fout = mgr.openFd("device/device_address.txt").createOutputStream();
    PrintWriter _fout = new PrintWriter(fout);
    _fout.println(address);
    Log.e("Glenn", address);

    _fout.close();
    fout.close();

    InputStream fin =  mgr.open("device/device_address.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));
    address = br.readLine(); 

    try {
        Log.e("Glenn:", address);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
} catch (IOException e) {
    e.printStackTrace();
    Log.e("Glenn", "error with OutputStream");
}
Run Code Online (Sandbox Code Playgroud)

前两次Log.e()调用打印的地址值是正确的值,实际上是设备MAC地址.但是,当我试图测试从刚刚写入的文件中读取的地址值时,NullPointerException已经在Log.e()调用中捕获了.这意味着从文件中读取的值是NULL.任何人都可以指出代码有什么问题吗?

android inputstream fileoutputstream bufferedreader android-assets

0
推荐指数
1
解决办法
266
查看次数