0

I have an app that allows the user to select a file from the file chooser. The problem lies when I try to turn that Uri to a File, it creates something that I can't use (/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108) I would like to get rid of everything before raw: but the right way. Where ever I try to copy from that file using InputStream, it doesn't copy anything. It's like the file doesn't exist. Any ideas?

public void chooseDatabaseFile() {
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    // Set your required file type
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Choose Database to Import"),GET_FILE_PATH);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GET_FILE_PATH && data != null) {
        if(resultCode == RESULT_OK){
            Uri currFileURI = data.getData();
            if(currFileURI == null) {
                return;
            }
            else{
                String databasePath = currFileURI.getPath();
                // TODO: Determine if actual database
                importDatabase(new File(databasePath));
                // File produces "/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108"
            }
        }
    }
}
Rayaarito
  • 400
  • 4
  • 21

1 Answers1

0

Have you tried changing your mimetype?

Otherwise take a look at:

Convert file: Uri to File in Android

Crainko
  • 41
  • 3
  • That's actually the answer I used to get the lin `String databasePath = currFileURI.getPath();`. The problem is that this also produces `/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108` when I'm getting something from the downloads folder. I'm almost positive that `document/raw:` is the problem here. But I don't want to just parse it. I'm sure there's a proper way to go around it that I just can't find – Rayaarito Oct 08 '19 at 14:59
  • Have you tried using the [Cursor](https://developer.android.com/reference/android/database/Cursor) i had a simmilar Problem when i had to support different android versions for loading Files – Crainko Oct 09 '19 at 06:43
  • I actually found the answer from another stackoverflow question (https://stackoverflow.com/questions/19834842/android-gallery-on-android-4-4-kitkat-returns-different-uri-for-intent-action/46485037#46485037). I'm still in the process of understanding it before posting it. Unless you want to give it a shot for the upvote! I just put it in my code yesterday to see if it would work and it did. – Rayaarito Oct 09 '19 at 12:41