0

I am trying to open a folder from Android N with no luck. This is my code:

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "xyx");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    file = FileProvider.getUriForFile(context, "ar.com.xyz.mymobile.provider", mediaStorageDir);
    intent.setData(file);
    context.startActivity(intent);
} else {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    file = Uri.fromFile(mediaStorageDir);
    intent.setDataAndType(file, "*.*");
    context.startActivity(intent);
}

Error:

No Activity found to handle Intent
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
jpussacq
  • 573
  • 9
  • 29

2 Answers2

1

There is no standard Intent action, on any version of Android, to view a directory.

Also, FWIW, *.* is not a MIME type.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi. And is it posible to open a folder with gallery viewer? Because my folder only has impages. Thanks! – jpussacq Apr 16 '18 at 13:56
  • 1
    @jpussacq: There is no standard `Intent` action for that either. You will need to create your own UI for that. – CommonsWare Apr 16 '18 at 19:45
1

Try the following example:

Demo13.class:---------

public class Demo13 extends AppCompatActivity {

private Button b;
private EditText edt;

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

    edt = (EditText) findViewById(R.id.edt);

    b = (Button) findViewById(R.id.b);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!edt.getText().toString().isEmpty()) {

                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES), edt.getText().toString());

                if(mediaStorageDir.exists()) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(mediaStorageDir), "resource/folder");
                    Intent chooser = Intent.createChooser(intent, "File");

                    if (intent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
                        startActivity(chooser);

                    } else {
                        createInfoDialog(Demo13.this, "Info", "Found no app that can handle this request. " +
                                "Please install a file browser/manager like ES File Explorer or browse the file yourself by following the path after finished Syn in the log above", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //nothing
                            }
                        }).show();
                        // if you reach this place, it means there is no file
                        // explorer app installed on your device that can handle this request.
                    }
                }else{
                    createInfoDialog(Demo13.this, "Info", "Sub-Folder not found", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // nothing
                        }
                    }).show();
                }
            }else{
                createInfoDialog(Demo13.this, "Info", "Please insert a valid sub-folder name", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // nothing
                    }
                }).show();
            }
        }
    });

}

public AlertDialog createInfoDialog(Context finalContext, String title, String message,
                                           DialogInterface.OnClickListener onOkListener) {
    AlertDialog a = new AlertDialog.Builder(finalContext).setTitle(
            title)
            .setMessage(message)
            .setNeutralButton("OK", onOkListener).create();

    return a;
}
}

demo13.xml:-----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter a valid sub-folder"
    android:id="@+id/edt"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAllCaps="false"
    android:layout_marginTop="50dp"
    android:text="Open"
    android:id="@+id/b"/>

</LinearLayout>
Brainnovo
  • 1,749
  • 2
  • 12
  • 17