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;
}
}
Aggregations