Intent.ACTION_OPEN_DOCUMENT_TREE (from API level 21) allow the user to pick a directory subtree. When invoked, the system will display the various DocumentsProvider instances installed on the device, letting the user navigate through them. Apps can fully manage documents within the returned directory.
This example call the method fromTreeUri() of android.support.v4.provider.DocumentFile to list files from the returned Uri, so we have to "Add Support Library, com.android.support:support-v4, to our Android Studio project".
com.blogspot.android_er.android_action_open_document_tree.MainActivity.java
package com.blogspot.android_er.android_action_open_document_tree;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.provider.DocumentFile;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int RQS_OPEN_DOCUMENT_TREE = 2;
TextView textInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInfo = (TextView)findViewById(R.id.info);
Button btnOpen = (Button)findViewById(R.id.opendocument);
btnOpen.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, RQS_OPEN_DOCUMENT_TREE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
textInfo.setText("");
if(resultCode == RESULT_OK && requestCode == RQS_OPEN_DOCUMENT_TREE){
Uri uriTree = data.getData();
textInfo.append(uriTree.toString() + "\n");
textInfo.append("=====================\n");
DocumentFile documentFile = DocumentFile.fromTreeUri(this, uriTree);
for (DocumentFile file : documentFile.listFiles()) {
textInfo.append(file.getName() + "\n");
if(file.isDirectory()){
textInfo.append("is a Directory\n");
}else{
textInfo.append(file.getType() + "\n");
}
textInfo.append("file.canRead(): " + file.canRead() + "\n");
textInfo.append("file.canWrite(): " + file.canWrite() + "\n");
textInfo.append(file.getUri() + "\n");
textInfo.append("---------------------\n");
}
}
}
}
layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Button
android:id="@+id/opendocument"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Intent.ACTION_OPEN_DOCUMENT_TREE" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
3 comments:
Hi friend, this code doesn't work to me in a preference fragment. Told me null object reference .. Do you know why??
Thanks
How to print the location of the current selected directory?
Post a Comment