Android txt:打开失败:ENOENT(没有这样的文件或目录)

时间:2020-05-03 07:02:49

标签: java android xml android-studio android-listview

我正在一个项目中,其中loading txt data来自手机存储and making a list view with that text data。我的项目在Android版本9.1上运行良好。但是,当我将同一项目运行到我的棒棒糖版本手机(5.1.1)中时,它不会加载文件,并且列表视图保持空白。

LogCat:

java.io.FileNotFoundException:下载/sampledata.txt:打开失败: ENOENT(无此文件或目录)

这仅在棒棒糖手机上发生,并且在其他版本中也可以正常使用。我如何摆脱这个问题?

主要活动

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";


//---- FOR LIST -----
ListView listView;
ArrayAdapter<String> adapter;
int count = 0;
String[] data;

BufferedReader bufferedReaderCounter;
BufferedReader bufferedReaderLoader;


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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
    }

    listView = findViewById(R.id.listview);

}

public void loadclick(View view) {
    filesearch();
}

private void filesearch(){
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/*");
    startActivityForResult(intent, 42);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 42 && resultCode == Activity.RESULT_OK) {

        if (data != null) {
            Uri uri = data.getData();
            String path = uri.getPath();
            path = path.substring(path.indexOf(":") + 1);
            Toast.makeText(this, " " + path, Toast.LENGTH_SHORT).show();
            loaddatafromfile(path);

        }
    }
}

private void loaddatafromfile(String path){

    File file = new File(path);
    try {
        file.mkdirs();

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        bufferedReaderCounter = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {

        //*** EXEPTION COMMING FROM HERE ****

        bufferedReaderLoader = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    try {
        while (bufferedReaderCounter.readLine() != null){
            count++;
        }
    }catch (Exception e){
    }

    data = new String[count];

    try {
        for (int i = 0; i<count; i++){
            data[i] = bufferedReaderLoader.readLine();
        }
    }catch (Exception e){
    }

    adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, data);
    listView.setAdapter(adapter);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 1000){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"Granted", Toast.LENGTH_SHORT).show();
        }
    }
}

0 个答案:

没有答案