The documentation says "Callers can set a document URI through EXTRA_INITIAL_URI
to indicate the initial location of documents navigator."
But it won't resolve EXTRA_INITIAL_URI
here. I tried setting the constant given for this as shown in the comment (EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI"
), but that does not work. How do I get it to resolve?
package com.ship.saftwo;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
public class MainActivity extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {navigateToAppDirectory();}});}
private static final int READ_REQUEST_CODE = 42;
// adding this does not help: public static final String EXTRA_INITIAL_URI = "android.provider.extra.INITIAL_URI";
public void navigateToAppDirectory() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + File.separator + getResources().getString(R.string.app_name) + File.separator);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_INITIAL_URI, uri);
//error message: Cannot resolve symbol 'EXTRA_INITIAL_URI'
startActivityForResult(intent, READ_REQUEST_CODE);
}
}
From DocumentsContract.EXTRA_INITIAL_URI:
Location should specify a document URI or a tree URI with document ID. If this URI identifies a non-directory, document navigator will attempt to use the parent of the document as the initial location.
As far as I'm aware
Uri.fromFile
, it needs to originate from a DocumentsProviderAssuming uri
was retrieved from Intent.ACTION_OPEN_DOCUMENT_TREE:
DocumentFile file = DocumentFile.fromTreeUri(context, uri);
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());
I was able to set it like this :
private fun requestUri() {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
.setType("application/text")
.putExtra(Intent.EXTRA_TITLE, "DEST_FILE_NAME")
.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.DIRECTORY_DOCUMENTS)
requestUri.launch(intent) // my start activity for result logic
}
private var requestUri = registerForActivityResult(StartActivityForResult()) { result ->
if (result != null && result.resultCode == Activity.RESULT_OK) {
result.data?.let { intent ->
intent.data?.let { fileUri ->
// u have a valid uri now
} ?: run {
// show some error Ui
}
}
}
}
in my app i can set DocumentsContract.EXTRA_INITIAL_URI to something like :
may this help some one :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With