7

I downloaded a file for user , now I want to navigate user to the specific folder where file was downloaded . I searched a lot and this was the best I could come up with

 public static void openASpecificFolderInFileManager(Context context, String path) {

    Intent i = new Intent(Intent.ACTION_VIEW);  
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setDataAndType(Uri.parse(path), "resource/folder");

    if (i.resolveActivityInfo(context.getPackageManager(), 0) != null) {
        context.startActivity(Intent.createChooser(i, "Open with"));
    } else {
        Toast.makeText(context, "File Manager not found ", Toast.LENGTH_SHORT).show();
    }

}

This works fine for specific file explorers (like ES filemanager) but doesn't work with default android file managers .

NOTE: I do not want to pick a file. I just want to open specific folder so user can view files

Is there any way I can achieve it with default file explorers ?

Manohar
  • 22,116
  • 9
  • 108
  • 144

1 Answers1

0

Test this to open the folder containing your text file, for example:

    public class fillEditText extends Activity {
        final int REQUEST_CODE=1;//your code for onActivityResult


    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() 
        + "/YourValidFolder/");
        intent.setDataAndType(uri, "text/plain");
        if (intent.resolveActivity(getPackageManager()) != null)
            startActivityForResult(Intent.createChooser(intent, getString("Select your file")), REQUEST_CODE);
    }

...}

Result after selecting your file:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Uri selectedfile_uri = data.getData();
            try {
                String text = readTextFromUri(selectedfile_uri);
                if (!text.isEmpty()) {
                    editText.setText(text);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

private String readTextFromUri(Uri uri) throws IOException {
        InputStream inputStream = getContentResolver().openInputStream(uri);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        inputStream.close();
        return stringBuilder.toString();
}
Jimmy Cram
  • 71
  • 2
  • 2