diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java index 099809986..cfe587929 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java @@ -1,87 +1,22 @@ package eu.kanade.tachiyomi.data.io; -import android.content.ContentResolver; import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.os.Build; -import android.os.ParcelFileDescriptor; -import android.provider.DocumentsContract; -import android.provider.MediaStore; import java.io.File; -import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOHandler { - /** - * Get full filepath of build in Android File picker. - * If Google Drive (or other Cloud service) throw exception and download before loading - */ - public static String getFilePath(Uri uri, ContentResolver resolver, Context context) { - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - String filePath = ""; - String wholeID = DocumentsContract.getDocumentId(uri); - - //Ugly work around. In sdk version Kitkat or higher external getDocumentId request will have no content:// - if (wholeID.split(":").length == 1) - throw new IllegalArgumentException(); - - // Split at colon, use second item in the array - String id = wholeID.split(":")[1]; - - String[] column = {MediaStore.Images.Media.DATA}; - - // where id is equal to - String sel = MediaStore.Images.Media._ID + "=?"; - - Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - column, sel, new String[]{id}, null); - - int columnIndex = cursor != null ? cursor.getColumnIndex(column[0]) : 0; - - if (cursor != null ? cursor.moveToFirst() : false) { - filePath = cursor.getString(columnIndex); - } - cursor.close(); - return filePath; - } else { - String[] fields = {MediaStore.Images.Media.DATA}; - - Cursor cursor = resolver.query(uri, fields, null, null, null); - - if (cursor == null) - return null; - - cursor.moveToFirst(); - String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); - cursor.close(); - - return path; - } - } catch (IllegalArgumentException e) { - //This exception is thrown when Google Drive. Try to download file - return downloadMediaAndReturnPath(uri, resolver, context); - } - } - private static String getTempFilename(Context context) throws IOException { File outputDir = context.getCacheDir(); File outputFile = File.createTempFile("temp_cover", "0", outputDir); return outputFile.getAbsolutePath(); } - private static String downloadMediaAndReturnPath(Uri uri, ContentResolver resolver, Context context) { - if (uri == null) return null; - FileInputStream input = null; + public static String downloadMediaAndReturnPath(FileInputStream input, Context context) { FileOutputStream output = null; try { - ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r"); - FileDescriptor fd = pfd != null ? pfd.getFileDescriptor() : null; - input = new FileInputStream(fd); String tempFilename = getTempFilename(context); output = new FileOutputStream(tempFilename); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index 6b5166cce..731857cbb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -26,6 +26,7 @@ import kotlinx.android.synthetic.main.fragment_library.* import nucleus.factory.RequiresPresenter import org.greenrobot.eventbus.EventBus import java.io.File +import java.io.FileInputStream import java.io.IOException /** @@ -316,12 +317,12 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_OPEN) { selectedCoverManga?.let { manga -> - // Get the file's content URI from the incoming Intent - val selectedImageUri = data.data + // Get the file's input stream from the incoming Intent + val inputStream = context.contentResolver.openInputStream(data.data) // Convert to absolute path to prevent FileNotFoundException - val result = IOHandler.getFilePath(selectedImageUri, - context.contentResolver, context) + val result = IOHandler.downloadMediaAndReturnPath(inputStream as FileInputStream, + context) // Get file from filepath val picture = File(result ?: "")