Why it is not opening defined folder via intent in Android?

天大地大妈咪最大 提交于 2020-01-16 09:09:31

问题


This code is redirecting to drive with open navigation but not opening actual given path

OLD Code

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/foldername/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "text/csv");
startActivity(intent);

The path value I am getting through uri is:
/storage/emulated/0/myfolder

NEW Code

Intent resultIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
resultIntent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = 
Uri.parse(Environment.getExternalStorageDirectory().getPath() + 
"/myfolder/");
resultIntent.setType("*/*");
resultIntent.putExtra("android.provider.extra.INITIAL_URI", uri);
// show the entire internal storage tree
//resultIntent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivity(resultIntent);

Let me know the issue where is the problem in this code.


回答1:


private static final int READ_REQUEST_CODE = 10;
...
/**
 * Fires an intent to spin up the "file chooser" UI and select an image.
 */
public void performFileSearch() {

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType(uri, "text/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}

After the user selects a document in the picker, onActivityResult() gets called. The resultData parameter contains the URI that points to the selected document. Extract the URI using getData(). When you have it, you can use it to retrieve the document the user wants.

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) {

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

        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            showImage(uri);
        }
    }
}


来源:https://stackoverflow.com/questions/58976433/why-it-is-not-opening-defined-folder-via-intent-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!