Search in sources :

Example 11 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project Anki-Android by ankidroid.

the class CardContentProvider method insertMediaFile.

private Uri insertMediaFile(ContentValues values, Collection col) {
    // Insert media file using libanki.Media.addFile and return Uri for the inserted file.
    Uri fileUri = Uri.parse(values.getAsString(FlashCardsContract.AnkiMedia.FILE_URI));
    String preferredName = values.getAsString(FlashCardsContract.AnkiMedia.PREFERRED_NAME);
    try {
        ContentResolver cR = mContext.getContentResolver();
        Media media = col.getMedia();
        // idea, open input stream and save to cache directory, then
        // pass this (hopefully temporary) file to the media.addFile function.
        // return eg "jpeg"
        String fileMimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(cR.getType(fileUri));
        // should we be enforcing strict mimetypes? which types?
        File tempFile;
        File externalCacheDir = mContext.getExternalCacheDir();
        if (externalCacheDir == null) {
            Timber.e("createUI() unable to get external cache directory");
            return null;
        }
        File tempMediaDir = new File(externalCacheDir.getAbsolutePath() + "/temp-media");
        if (!tempMediaDir.exists() && !tempMediaDir.mkdir()) {
            Timber.e("temp-media dir did not exist and could not be created");
            return null;
        }
        try {
            tempFile = File.createTempFile(// the beginning of the filename.
            preferredName + "_", // this is the extension, if null, '.tmp' is used, need to get the extension from MIME type?
            "." + fileMimeType, tempMediaDir);
            tempFile.deleteOnExit();
        } catch (Exception e) {
            Timber.w(e, "Could not create temporary media file. ");
            return null;
        }
        FileUtil.internalizeUri(fileUri, tempFile, cR);
        String fname = media.addFile(tempFile);
        Timber.d("insert -> MEDIA: fname = %s", fname);
        File f = new File(fname);
        Timber.d("insert -> MEDIA: f = %s", f);
        Uri uriFromF = Uri.fromFile(f);
        Timber.d("insert -> MEDIA: uriFromF = %s", uriFromF);
        return Uri.fromFile(new File(fname));
    } catch (IOException | EmptyMediaException e) {
        Timber.w(e, "insert failed from %s", fileUri);
        return null;
    }
}
Also used : Media(com.ichi2.libanki.Media) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) EmptyMediaException(com.ichi2.libanki.exception.EmptyMediaException) JSONException(com.ichi2.utils.JSONException) DeckRenameException(com.ichi2.libanki.backend.exception.DeckRenameException) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) IOException(java.io.IOException) ContentResolver(android.content.ContentResolver) EmptyMediaException(com.ichi2.libanki.exception.EmptyMediaException)

Aggregations

Deck (com.ichi2.libanki.Deck)4 IOException (java.io.IOException)4 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)3 JSONException (com.ichi2.utils.JSONException)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SuppressLint (android.annotation.SuppressLint)2 SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)2 Button (android.widget.Button)2 PickStringDialogFragment (com.ichi2.anki.multimediacard.activity.PickStringDialogFragment)2 NoSuchDeckException (com.ichi2.libanki.exception.NoSuchDeckException)2 FunctionalInterfaces (com.ichi2.utils.FunctionalInterfaces)2 JSONObject (com.ichi2.utils.JSONObject)2 RustCleanup (net.ankiweb.rsdroid.RustCleanup)2 ContentResolver (android.content.ContentResolver)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Resources (android.content.res.Resources)1 Cursor (android.database.Cursor)1