use of android.webkit.MimeTypeMap in project instructure-android by instructure.
the class FileUploadUtils method getFileExtensionFromMimeType.
public static String getFileExtensionFromMimeType(String mimeType) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = mime.getExtensionFromMimeType(mimeType);
if (extension == null) {
return "";
}
return extension;
}
use of android.webkit.MimeTypeMap in project Zom-Android by zom.
the class SystemServices method getMimeType.
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
if (type == null)
if (url.endsWith("jpg"))
return MIME_TYPE_JPEG;
else if (url.endsWith("png"))
return MIME_TYPE_PNG;
return type;
}
use of android.webkit.MimeTypeMap in project library by cgogolin.
the class BibtexAdapter method openExternally.
public void openExternally(Context context, Uri uri) {
if (uri == null | context == null)
return;
// Determine mime type
MimeTypeMap map = MimeTypeMap.getSingleton();
String extension = "";
String uriString = uri.toString();
if (uriString.lastIndexOf(".") != -1)
extension = uriString.substring((uriString.lastIndexOf(".") + 1), uriString.length());
String type = map.getMimeTypeFromExtension(extension);
// Start application to open the file and grant permissions
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.setDataAndType(uri, type);
try {
context.startActivity(intent);
if (android.os.Build.VERSION.SDK_INT >= 19) {
// Taken from http://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps
try {
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
}
} catch (Exception e) {
}
}
} catch (ActivityNotFoundException e) {
Toast.makeText(context, context.getString(R.string.no_application_to_view_files_of_type) + " " + type + ".", Toast.LENGTH_SHORT).show();
}
}
use of android.webkit.MimeTypeMap in project Tusky by tuskyapp.
the class ComposeActivity method saveMedia.
@Nullable
private List<String> saveMedia(@Nullable ArrayList<String> existingUris) {
File imageDirectory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File videoDirectory = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
if (imageDirectory == null || !(imageDirectory.exists() || imageDirectory.mkdirs())) {
Log.e(TAG, "Image directory is not created.");
return null;
}
if (videoDirectory == null || !(videoDirectory.exists() || videoDirectory.mkdirs())) {
Log.e(TAG, "Video directory is not created.");
return null;
}
ContentResolver contentResolver = getContentResolver();
ArrayList<File> filesSoFar = new ArrayList<>();
ArrayList<String> results = new ArrayList<>();
for (QueuedMedia item : mediaQueued) {
/* If the media was already saved in a previous draft, there's no need to save another
* copy, just add the existing URI to the results. */
if (existingUris != null) {
String uri = item.uri.toString();
int index = existingUris.indexOf(uri);
if (index != -1) {
results.add(uri);
continue;
}
}
// Otherwise, save the media.
File directory;
switch(item.type) {
default:
case IMAGE:
directory = imageDirectory;
break;
case VIDEO:
directory = videoDirectory;
break;
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String mimeType = contentResolver.getType(item.uri);
MimeTypeMap map = MimeTypeMap.getSingleton();
String fileExtension = map.getExtensionFromMimeType(mimeType);
String filename = String.format("Tusky_Draft_Media_%s.%s", timeStamp, fileExtension);
File file = new File(directory, filename);
filesSoFar.add(file);
boolean copied = copyToFile(contentResolver, item.uri, file);
if (!copied) {
/* If any media files were created in prior iterations, delete those before
* returning. */
for (File earlierFile : filesSoFar) {
boolean deleted = earlierFile.delete();
if (!deleted) {
Log.i(TAG, "Could not delete the file " + earlierFile.toString());
}
}
return null;
}
Uri uri = FileProvider.getUriForFile(this, "com.keylesspalace.tusky.fileprovider", file);
results.add(uri.toString());
}
return results;
}
use of android.webkit.MimeTypeMap in project Tusky by tuskyapp.
the class ComposeActivity method uploadMedia.
private void uploadMedia(final QueuedMedia item) {
item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
String mimeType = getContentResolver().getType(item.uri);
MimeTypeMap map = MimeTypeMap.getSingleton();
String fileExtension = map.getExtensionFromMimeType(mimeType);
final String filename = String.format("%s_%s_%s.%s", getString(R.string.app_name), String.valueOf(new Date().getTime()), StringUtils.randomAlphanumericString(10), fileExtension);
byte[] content = item.content;
if (content == null) {
InputStream stream;
try {
stream = getContentResolver().openInputStream(item.uri);
} catch (FileNotFoundException e) {
Log.d(TAG, Log.getStackTraceString(e));
return;
}
content = MediaUtils.inputStreamGetBytes(stream);
IOUtils.closeQuietly(stream);
if (content == null) {
return;
}
}
if (mimeType == null)
mimeType = "multipart/form-data";
item.preview.setProgress(0);
ProgressRequestBody fileBody = new ProgressRequestBody(content, MediaType.parse(mimeType), // If request body logging is enabled, pass true
false, new // may reference activity longer than I would like to
ProgressRequestBody.UploadCallback() {
int lastProgress = -1;
@Override
public void onProgressUpdate(final int percentage) {
if (percentage != lastProgress) {
runOnUiThread(() -> item.preview.setProgress(percentage));
}
lastProgress = percentage;
}
});
MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, fileBody);
item.uploadRequest = mastodonApi.uploadMedia(body);
item.uploadRequest.enqueue(new Callback<Attachment>() {
@Override
public void onResponse(@NonNull Call<Attachment> call, @NonNull retrofit2.Response<Attachment> response) {
if (response.isSuccessful()) {
onUploadSuccess(item, response.body());
} else {
Log.d(TAG, "Upload request failed. " + response.message());
onUploadFailure(item, call.isCanceled());
}
}
@Override
public void onFailure(@NonNull Call<Attachment> call, @NonNull Throwable t) {
Log.d(TAG, "Upload request failed. " + t.getMessage());
onUploadFailure(item, call.isCanceled());
}
});
}
Aggregations