HBiSoft / PickiT

An Android library that returns real paths from Uri's
MIT License
292 stars 54 forks source link

Problems with naming files from google drive. Not supported yandex disk. #13

Closed pavelperc closed 4 years ago

pavelperc commented 4 years ago

Your lib has some problems with getting the name of file from google drive. It always named as "tempFile" with no extension. Also when picking files from YandexDisk drive (https://play.google.com/store/apps/details?id=ru.yandex.disk&hl=en) it returnes wasSuccessfull false and reason "column '_data' does not exist". Also I would prefer to store files in external cache directory.

I didn't change the lib, but here is some code how I fixed both problems on my device:

@Override
    public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String Reason) {
        if (!wasSuccessful) {
            String name = getFileName(originalFileUri);
            File file = new File(getExternalCacheDir(), name);
            try {
                saveFile(getContentResolver().openInputStream(originalFileUri), file);
            } catch (FileNotFoundException e) {
                return;
            }
            path = file.getPath();
        } 
        else if (wasDriveFile) {
            String name = getFileName(originalFileUri);
            File newFile = new File(getExternalCacheDir(), name);
            File oldFile = new File(path);
            oldFile.renameTo(newFile);
            path = newFile.getPath();
        }
        processFile(path);
    }

// https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
private String getFileName(Uri uri) {
        String result = null;
        if (uri.getScheme().equals("content")) {
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            try {
                if (cursor != null && cursor.moveToFirst()) {
                    result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                }
            } finally {
                cursor.close();
            }
        }
        if (result == null) {
            result = uri.getPath();
            int cut = result.lastIndexOf('/');
            if (cut != -1) {
                result = result.substring(cut + 1);
            }
        }
        return result;
    }

private void saveFile(InputStream input, File file) {
        // todo: just saves the file
    }
HBiSoft commented 4 years ago

Please check the latest release - https://github.com/HBiSoft/PickiT/releases/tag/0.1.9

I tested with Yandex and it is working. The name will now be the same as the original file, this should also include the file extension (if it is available). Please have a look at this - https://stackoverflow.com/a/53132045/5550161

I will be closing this issue, please open it again if you have any issues.

pavelperc commented 4 years ago

@HBiSoft I'm sorry. I used 1.0.8. I will check 1.0.9. Please, update the description on android-arsenal.

HBiSoft commented 4 years ago

@pavelperc I have no control over what is shown on android arsenal. If you want to know when the library has updated, you can "watch" it.