Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set the initial location of documents navigator using Storage Access Framework (SAF)

Tags:

android

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);
    }
}
like image 551
Reve Archer Avatar asked May 22 '18 05:05

Reve Archer


2 Answers

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

  • You cannot specify a starting location using an arbitrary URI, e.g. Uri.fromFile, it needs to originate from a DocumentsProvider
  • You cannot specify a starting location on API 25 and below

Assuming uri was retrieved from Intent.ACTION_OPEN_DOCUMENT_TREE:

DocumentFile file = DocumentFile.fromTreeUri(context, uri);
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());
like image 72
Alex Baker Avatar answered Nov 18 '22 06:11

Alex Baker


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 :

  • Environment.DIRECTORY_DOCUMENTS
  • Environment.DIRECTORY_DOWNLOADS

may this help some one :)

like image 2
issamux Avatar answered Nov 18 '22 07:11

issamux