无法从内部根存储中读取文件

时间:2016-11-17 04:01:49

标签: android

我有一个扎根的Android手机,我的客户端有8个平板电脑(无根)。他们将在明天晚上使用它来做一些从Android平板电脑判断的比赛。我做了一个应用程序,从我的内部存储器的根文件夹上的自定义文件夹中读取文件,该文件夹在我的root用户手机上运行正常,但在客户端无根据平板电脑上运行。

据他们说,所有文件都将存储在内存中最多3GB。我'被卡住的人真的需要帮助,并建议我该怎么做?

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        ImageView welcomeLogo = (ImageView) findViewById(R.id.welcomeLogo);
        welcomeLogo.setImageResource(R.drawable.iba);

        //Get the text file
        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        File file = new File(dir, "lvi/names.txt");
        //Read text from file
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
            br.close();

            //Find the view by its id
            TextView tv = (TextView)findViewById(R.id.welcomeJudge);
            //Set the text
            tv.setText("Welcome " + text.toString());
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }

        //When the enter button is click goto the dashboard activity
        button = (Button) findViewById(R.id.btnEnter);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent dashboardIntent = new Intent(MainActivity.this, dashboard.class);
                startActivity(dashboardIntent);
            }
        });
    }
}

我的清单上的权限

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

1 个答案:

答案 0 :(得分:0)

Android 6.0(API级别23)您必须设置运行时权限

 if (Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions((BaseActivity) context,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK);
    } else {
        ReadMethod();//your custom method 


    }

onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK:

        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            ReadMethod();//your custom method 
        }
        break;


    }
 }