亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從內存中讀取文件:“/sys/class/”和“/dev/”文件夾

從內存中讀取文件:“/sys/class/”和“/dev/”文件夾

森林海 2022-06-23 15:56:40
我想知道如何讀取內部存儲器中某些文件的值,但這些文件不在“/data/data/myapp/files”文件夾中,它們位于“/dev/”和“/”文件夾中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;        }    }}
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

由于您的應用程序具有 root 權限,您可能能夠訪問/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;

    }

}

您可以在 a 中設置文件內容TextView(前提是它是文本文件):


TextView Tempview;

Tempview = (TextView) findViewById( R.id.temperatura );

Tempview.setText(new String(readFile(path), Charset.forName("UTF-8")));


查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號