Search in sources :

Example 46 with MimeTypeMap

use of android.webkit.MimeTypeMap in project iNaturalistAndroid by inaturalist.

the class ObservationEditor method getExtension.

public static String getExtension(Context context, Uri uri) {
    String extension;
    // Check uri format to avoid null
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        // If scheme is a content
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        // If scheme is a File
        // This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
    }
    return extension;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) File(java.io.File)

Example 47 with MimeTypeMap

use of android.webkit.MimeTypeMap in project android-classyshark by google.

the class MainActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    final ArrayList<AppListNode> apps = new ArrayList<>();
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
    for (Object object : pkgAppsList) {
        ResolveInfo info = (ResolveInfo) object;
        File file = new File(info.activityInfo.applicationInfo.publicSourceDir);
        AppListNode aln = new AppListNode();
        aln.name = info.activityInfo.applicationInfo.processName.toString();
        aln.file = file;
        apps.add(aln);
    }
    Collections.sort(apps);
    final StableArrayAdapter adapter = new StableArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, convert(apps));
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MimeTypeMap myMime = MimeTypeMap.getSingleton();
            Intent newIntent = new Intent(MainActivity.this, ClassesListActivity.class);
            String mimeType = myMime.getMimeTypeFromExtension("apk");
            newIntent.setDataAndType(Uri.fromFile(apps.get(position).file), mimeType);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            newIntent.putExtra(APP_NAME, apps.get(position).name);
            try {
                startActivity(newIntent);
            } catch (ActivityNotFoundException e) {
            }
        }
    });
}
Also used : StableArrayAdapter(com.google.classysharkandroid.adapters.StableArrayAdapter) ArrayList(java.util.ArrayList) Intent(android.content.Intent) MimeTypeMap(android.webkit.MimeTypeMap) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ResolveInfo(android.content.pm.ResolveInfo) ActivityNotFoundException(android.content.ActivityNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) AdapterView(android.widget.AdapterView) File(java.io.File)

Example 48 with MimeTypeMap

use of android.webkit.MimeTypeMap in project HL4A by HL4A.

the class AVFile method getMimeType.

/**
 * @param url
 * @return
 * @deprecated replaced by {@link AVUtils#getMimeTypeFromLocalFile(String)} or
 * {@link AVUtils#getMimeTypeFromUrl(String)}
 */
public static String getMimeType(String url) {
    String type = DEFAULTMIMETYPE;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    if (type == null) {
        type = DEFAULTMIMETYPE;
    }
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 49 with MimeTypeMap

use of android.webkit.MimeTypeMap in project android-aosp-mms by slvn.

the class UriImage method initFromFile.

private void initFromFile(Context context, Uri uri) {
    mPath = uri.getPath();
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(mPath);
    if (TextUtils.isEmpty(extension)) {
        // getMimeTypeFromExtension() doesn't handle spaces in filenames nor can it handle
        // urlEncoded strings. Let's try one last time at finding the extension.
        int dotPos = mPath.lastIndexOf('.');
        if (0 <= dotPos) {
            extension = mPath.substring(dotPos + 1);
        }
    }
    mContentType = mimeTypeMap.getMimeTypeFromExtension(extension);
    // It's ok if mContentType is null. Eventually we'll show a toast telling the
    // user the picture couldn't be attached.
    buildSrcFromPath();
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 50 with MimeTypeMap

use of android.webkit.MimeTypeMap in project FileDownloaderManager by arlyxiao.

the class DownloadLib method get_mime_type.

// protected void open_file() {
// String full_file_path = Environment.getExternalStorageDirectory().getAbsolutePath()
// + save_file_path;
// Log.i("要打开的文件 ", full_file_path);
// File file = new File(full_file_path);
// 
// try {
// Intent intent = new Intent(Intent.ACTION_VIEW);
// // intent.setAction(Intent.ACTION_VIEW);
// intent.setDataAndType(Uri.fromFile(file), get_mime_type(file.getAbsolutePath()));
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(intent);
// } catch (Exception e) {
// Log.i("文件打开错误 ", e.getMessage());
// }
// }
private String get_mime_type(String url) {
    String[] parts = url.split("\\.");
    String extension = parts[parts.length - 1];
    String type = null;
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Aggregations

MimeTypeMap (android.webkit.MimeTypeMap)51 File (java.io.File)12 Intent (android.content.Intent)7 DownloadManager (android.app.DownloadManager)6 Uri (android.net.Uri)6 SimpleDateFormat (java.text.SimpleDateFormat)4 ArrayList (java.util.ArrayList)4 SuppressLint (android.annotation.SuppressLint)3 ActivityNotFoundException (android.content.ActivityNotFoundException)3 ResolveInfo (android.content.pm.ResolveInfo)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Date (java.util.Date)3 IntentFilter (android.content.IntentFilter)2 Cursor (android.database.Cursor)2 View (android.view.View)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 MultipartBody (okhttp3.MultipartBody)2