From Uri to Environment.DIRECTORY_DCIM - java

A user chooses a directory through the following code:
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 8010);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath = data.getData().toString();
break;
}
}
His directory is stored in mypath as following (e.g. with DCIM):
content://com.android.externalstorage.documents/tree/primary%3ADCIM
Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM to Environment.DIRECTORY_DCIMand how?
Asked differently, I would like to get the variable Environment.DIRECTORY_DCIM for mypath when the User chooses the DCIM directory. How can one do that?

Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM to Environment.DIRECTORY_DCIMand how?
No. The Uri that you get back does not have to correspond to a file (e.g., it points to a document in Google Drive). Even if it corresponds to a file, it does not have to reside in a directory that is tied to Environment (e.g., it points to a file in a custom directory).
Please just use the Uri.

Related

i want to get actual file path of an existing file from device

i want to get actual file path of an existing file from device
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
switch (requestCode){
case 10:
if(resultCode==RESULT_OK){
String path = data.getData().getPath();
txt_pathShow.setText(path);
}}}}

How to get file path for pdf in android Marshmallow

I tried to get path from
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"), 2);
But I did't get path from Marshamallow, I can get path from lollipop.
How I can get file path from internal storage?
I think you will have to use file provider for that.
Refer this.
https://developer.android.com/reference/android/support/v4/content/FileProvider
When you start an activity using startActivityForResult, the calling activity will get the results (if any) in onActivityResult. You should override that method in your calling activity...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
}
This will method will get called as soon as the results are returned from the activity started by startActivityForResult. You can use the requestCode parameter to match (or filter out) the results you are expecting. In your case, you should check that requestCode is equal to 2 which is the value you provided in startActivityForResult.
If the requestCode is equal to 2, then you know that you need to process a PDF file. The intent parameter "could" have the path of that PDF file...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == 2 && data != null){
//This is the uri to the file (which could be null)
Uri uri = data.getData();
if(uri != null){
Log.i("LOG_TAG", String.format("uri path = %s", uri));
}
}
}
Now, to browse and select files you will do it the following way (Android Kitkat and above...I think)...
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent, SELECT_FILE_REQUEST_CODE);
Then you receive the selected PDF uri as mentioned above

Android-what to do with chosen image?

I have the following code on my button, which allows me to click a button and choose a PDF file. How do I get the path of the PDF file, and actually do something with the file (because right now I can only click on the file and nothing happens). I want the image to be put in an API do to something to the image.
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
You must override the onActivityResult in order to get what you need.
You will be able to get what you want (the file path) with the resulting intent. Like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(resultCode == RESULT_OK) {
Uri fileUri = intent.getData();
}
}

How does one read a file using the Scoped Directory Access permission system?

I'm trying to access my SD Card on Android 7.0 (Samsung S7) and I found that this is a possible way to get permission for it: https://developer.android.com/training/articles/scoped-directory-access
I wrote the following code
public class MainActivity extends AppCompatActivity {
Intent sdCardIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
StorageVolume sdCARD = null;
for (StorageVolume volume : sm.getStorageVolumes()) {
if (volume.getDescription(this.getApplicationContext()).equals("SD card")) {
sdCARD = volume;
}
}
if (sdCARD == null) {
return;
}
sdCardIntent = sdCARD.createAccessIntent(null);
startActivityForResult(sdCardIntent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Access sd card?
}
}
When run, I get the permission prompt and it seems to work since I get the event in onActivityResult but I haven't found how to access any file or directory on the SD card.
Do I have to use the URI I get from the Intent result?
Do I have to use the URI I get from the Intent result?
Yes. That Uri should represent a document tree, and so you can use DocumentFile.fromTreeUri() to get a DocumentFile on that tree. From there, you can iterate over the contents and get Uri objects to individual pieces of content. Use a ContentResolver and its openInputStream() method to read in any such piece of content.
However:
Please do I/O on background threads
If you wish to pass the Uri to another component (e.g., activity, service), even within your own app, be sure to include FLAG_GRANT_READ_URI_PERMISSION and/or FLAG_GRANT_WRITE_URI_PERMISSION to your Intent

How to browse folder in android and get the path of folder selected

When I click a button,show a file browser,I can choose a folder and return it's path.I get this path to copy file to that path.
But I have no idea of How I could implement this.
I have yet looking for this question in Stackoverflow but I haven't find a clear answer to my question.
I saw some libary of filebrowserview like "https://github.com/psaravan/FileBrowserView",but not work.
Use intent for that!
First start start activity for result like this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Override this method in your activity, it will get called when activity you just started returns. You can handle result codes such as canceled or successful.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
//TODO handle your request here
super.onActivityResult(requestCode, resultCode, data);
}
Another approach is to use library such as NoNonsense-FilePicker.

Categories

Resources