Merge pull request #182 from NoodleMage/cover_edit_fix

Cover change fix (hopefully :-)
This commit is contained in:
inorichi 2016-02-25 16:11:17 +01:00
commit 4dde6d1a31
2 changed files with 6 additions and 70 deletions

View file

@ -1,87 +1,22 @@
package eu.kanade.tachiyomi.data.io; package eu.kanade.tachiyomi.data.io;
import android.content.ContentResolver;
import android.content.Context; 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.File;
import java.io.FileDescriptor;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
public class IOHandler { 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 { private static String getTempFilename(Context context) throws IOException {
File outputDir = context.getCacheDir(); File outputDir = context.getCacheDir();
File outputFile = File.createTempFile("temp_cover", "0", outputDir); File outputFile = File.createTempFile("temp_cover", "0", outputDir);
return outputFile.getAbsolutePath(); return outputFile.getAbsolutePath();
} }
private static String downloadMediaAndReturnPath(Uri uri, ContentResolver resolver, Context context) { public static String downloadMediaAndReturnPath(FileInputStream input, Context context) {
if (uri == null) return null;
FileInputStream input = null;
FileOutputStream output = null; FileOutputStream output = null;
try { try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd != null ? pfd.getFileDescriptor() : null;
input = new FileInputStream(fd);
String tempFilename = getTempFilename(context); String tempFilename = getTempFilename(context);
output = new FileOutputStream(tempFilename); output = new FileOutputStream(tempFilename);

View file

@ -26,6 +26,7 @@ import kotlinx.android.synthetic.main.fragment_library.*
import nucleus.factory.RequiresPresenter import nucleus.factory.RequiresPresenter
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import java.io.File import java.io.File
import java.io.FileInputStream
import java.io.IOException import java.io.IOException
/** /**
@ -316,12 +317,12 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_OPEN) { if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_OPEN) {
selectedCoverManga?.let { manga -> selectedCoverManga?.let { manga ->
// Get the file's content URI from the incoming Intent // Get the file's input stream from the incoming Intent
val selectedImageUri = data.data val inputStream = context.contentResolver.openInputStream(data.data)
// Convert to absolute path to prevent FileNotFoundException // Convert to absolute path to prevent FileNotFoundException
val result = IOHandler.getFilePath(selectedImageUri, val result = IOHandler.downloadMediaAndReturnPath(inputStream as FileInputStream,
context.contentResolver, context) context)
// Get file from filepath // Get file from filepath
val picture = File(result ?: "") val picture = File(result ?: "")