我想知道如何读取内部内存内某些文件的值,但是这些文件位于"/data/data/data/data/m myapp/files"文件夹中,它们位于"/dev/"one_answers"/sys/class"文件夹中。
import...
public class Calentando extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView( R.layout.activity_calentando );
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private byte[] readFile(String path) {
File file = new File( "/sys/class/gpio/gpio33/value" );
try (FileInputStream fis = new FileInputStream( file );
BufferedInputStream bis = new BufferedInputStream( fis )) {
byte[] buffer = new byte[4096];
int bytesRead;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((bytesRead = bis.read( buffer )) != -1) {
baos.write( buffer, 0, bytesRead );
TextView Tempview = (TextView) findViewById( R.id.temperatura );
Tempview.setText( new String( readFile( path ), Charset.forName( "UTF-8" ) ) );
}
return baos.toByteArray();
} catch (IOException e) {
// handle the exception
return null;
}
}
}
特别感谢@nandsito
由于您的应用程序已有根许可,因此您可以访问/dev
和/sys/class
目录。
您可以列出目录内容:
new File(path).listFiles();
您可以读取二进制文件内容:
private byte[] readFile(String path) {
File file = new File(path);
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis)) {
byte[] buffer = new byte[4096];
int bytesRead;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((bytesRead = bis.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
} catch (IOException e) {
// handle the exception
return null;
}
}
您可以在TextView
中设置文件内容(提供是文本文件):
TextView Tempview;
Tempview = (TextView) findViewById( R.id.temperatura );
Tempview.setText(new String(readFile(path), Charset.forName("UTF-8")));