1

He is my code:

File f; FileInputStream inputStream; byte[] buffer = null; try { f=new File("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); if(f.exists()){ inputStream = new FileInputStream(f); inputStream.read(buffer); } Log.i("informacja","Czytam"); } catch (Exception e) { // TODO Auto-generated catch block Log.i("informacja",e.toString()); } 

File path is correct, File exists. But always in inputStream.read(buffer) i get NullPointerException. This is part of my Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

Can anyone suggest me where I made a mistake?

@Update. Thanks for the code snippet. Now i can read but: this is what I get : [B@41a743c8 What I should get: 1000000

4
  • Is it really an external storage path? Can I suggest you to check external path? Look there: developer.android.com/guide/topics/data/data-storage.html Commented May 28, 2013 at 12:14
  • I dont think so. They are permissions -r--. And more i can read this file with antek explorer Commented May 28, 2013 at 12:26
  • I apologize. You were right Commented May 28, 2013 at 12:32
  • I edited my answer, please check it :D Commented May 28, 2013 at 12:45

3 Answers 3

2

I was able to read the output of cat

 ProcessBuilder cmd; String result = ""; try { String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" }; cmd = new ProcessBuilder(args); Process process = cmd.start(); InputStream in = process.getInputStream(); byte[] re = new byte[32768]; int read = 0; while ( (read = in.read(re, 0, 32768)) != -1) { String string = new String(re, 0, read); Log.e(getClass().getSimpleName(), string); result += string; } in.close(); } catch (IOException ex) { ex.printStackTrace(); } return result; 

and open it as normal file

 String result = ""; try { File file = new File("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); InputStream in = new FileInputStream(file); byte[] re = new byte[32768]; int read = 0; while ( (read = in.read(re, 0, 32768)) != -1) { String string = new String(re, 0, read); Log.e(getClass().getSimpleName(), string); result += string; } in.close(); } catch (IOException ex) { ex.printStackTrace(); } return result; 
Sign up to request clarification or add additional context in comments.

Comments

1

Android applications don't run as root on an unmodified device so you cant read from /sys folder without rooting the device.

Comments

1

The problem lies here --> byte[] buffer = null; and inputStream.read(buffer);

the buffer is still null, try this one :

 InputStream is = new FileInputStream(f); byte[] b = new byte[is.available()]; is.read(b); 

hope this helps, good luck ^^

P.S. Don't forget to set all necessary permission

Edit :

To read the file, you will need to parse it. The string result from the content of the string can be gotten with :String fileContent = new String(b); //the byte we read earlier

from there, you must parse the file to get the value you need. In order to parse the file, you will have to know the file structure and create parsing method as needed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.