mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-30 00:14:59 -05:00
Merge pull request #182 from NoodleMage/cover_edit_fix
Cover change fix (hopefully :-)
This commit is contained in:
commit
4dde6d1a31
2 changed files with 6 additions and 70 deletions
|
@ -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);
|
||||
|
|
|
@ -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<LibraryPresenter>(), 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 ?: "")
|
||||
|
|
Loading…
Reference in a new issue