Search in sources :

Example 11 with MimeTypeMap

use of android.webkit.MimeTypeMap in project Tusky by Vavassor.

the class ComposeActivity method uploadMedia.

private void uploadMedia(final QueuedMedia item) {
    item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
    final 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()), 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 = inputStreamGetBytes(stream);
        IOUtils.closeQuietly(stream);
        if (content == null) {
            return;
        }
    }
    RequestBody requestFile = RequestBody.create(MediaType.parse(mimeType), content);
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestFile);
    item.uploadRequest = mastodonAPI.uploadMedia(body);
    item.uploadRequest.enqueue(new Callback<Media>() {

        @Override
        public void onResponse(Call<Media> call, retrofit2.Response<Media> 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(Call<Media> call, Throwable t) {
            Log.d(TAG, t.getMessage());
            onUploadFailure(item, false);
        }
    });
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Media(com.keylesspalace.tusky.entity.Media) StringUtils.randomAlphanumericString(com.keylesspalace.tusky.util.StringUtils.randomAlphanumericString) MimeTypeMap(android.webkit.MimeTypeMap) Date(java.util.Date) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 12 with MimeTypeMap

use of android.webkit.MimeTypeMap in project ETSMobile-Android2 by ApplETS.

the class MoodleCourseDetailsFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle onSavedInstanceState) {
    super.onActivityCreated(onSavedInstanceState);
    receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                        String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        MimeTypeMap map = MimeTypeMap.getSingleton();
                        String ext = MimeTypeMap.getFileExtensionFromUrl(uriString);
                        String type = map.getMimeTypeFromExtension(ext);
                        if (type == null)
                            type = "*/*";
                        Intent openFile = new Intent(Intent.ACTION_VIEW);
                        openFile.setDataAndType(Uri.parse(uriString), type);
                        try {
                            startActivity(openFile);
                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(getActivity(), getString(R.string.cannot_open_file), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }
    };
    getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    queryMoodleCoreCourses(moodleCourseId);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) ActivityNotFoundException(android.content.ActivityNotFoundException) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) Cursor(android.database.Cursor) MimeTypeMap(android.webkit.MimeTypeMap) DownloadManager(android.app.DownloadManager)

Example 13 with MimeTypeMap

use of android.webkit.MimeTypeMap in project ETSMobile-Android2 by ApplETS.

the class MoodleCourseDetailsFragment method onRequestSuccess.

@Override
public void onRequestSuccess(Object o) {
    if (o instanceof MoodleCoreCourses) {
        MoodleCoreCourses moodleCoreCourses = (MoodleCoreCourses) o;
        // create empty data
        listDataSectionName = new HashMap<HeaderText, Object[]>();
        listDataHeader = new ArrayList<HeaderText>();
        int positionSection = 0;
        for (MoodleCoreCourse coreCourse : moodleCoreCourses) {
            listMoodleLinkModules = new ArrayList<MoodleCoreModule>();
            listMoodleResourceContents = new ArrayList<MoodleModuleContent>();
            for (MoodleCoreModule coreModule : coreCourse.getModules()) {
                if (coreModule.getModname().equals("folder")) {
                    if (coreModule.getContents() != null)
                        listMoodleResourceContents.addAll(coreModule.getContents());
                } else if (coreModule.getModname().equals("url") || coreModule.getModname().equals("forum")) {
                    listMoodleLinkModules.add(coreModule);
                } else if (coreModule.getModname().equals("resource")) {
                    listMoodleResourceContents.addAll(coreModule.getContents());
                }
            }
            Object[] finalArray = ArrayUtils.addAll(listMoodleLinkModules.toArray(), listMoodleResourceContents.toArray());
            if (finalArray.length != 0)
                listDataSectionName.put(new HeaderText(coreCourse.getName(), positionSection), finalArray);
            positionSection++;
        }
        listDataHeader.addAll(listDataSectionName.keySet());
        Collections.sort(listDataHeader, new Comparator<HeaderText>() {

            @Override
            public int compare(HeaderText headerText1, HeaderText headerText2) {
                if (headerText1.getPosition() < headerText2.getPosition()) {
                    return -1;
                } else if (headerText1.getPosition() == headerText2.getPosition()) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        expandableListMoodleAdapter = new ExpandableListMoodleSectionAdapter(getActivity(), listDataHeader, listDataSectionName);
        expListView.setAdapter(expandableListMoodleAdapter);
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Object object = expandableListMoodleAdapter.getChild(groupPosition, childPosition);
                if (object instanceof MoodleModuleContent) {
                    MoodleModuleContent item = (MoodleModuleContent) object;
                    String url = item.getFileurl() + "&token=" + ApplicationManager.userCredentials.getMoodleToken();
                    Uri uri = Uri.parse(url);
                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, item.getFilename());
                    //                      r.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    MimeTypeMap mimetype = MimeTypeMap.getSingleton();
                    String extension = FilenameUtils.getExtension(item.getFilename());
                    request.setMimeType(mimetype.getMimeTypeFromExtension(extension));
                    dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                    enqueue = dm.enqueue(request);
                    AnalyticsHelper.getInstance(getActivity()).sendActionEvent(getClass().getSimpleName(), TELECHARGE_FICHIER_MOODLE);
                }
                if (object instanceof MoodleCoreModule) {
                    MoodleCoreModule item = (MoodleCoreModule) object;
                    String url = "";
                    if (item.getModname().equals("url")) {
                        url = item.getContents().get(0).getFileurl();
                    } else {
                        url = item.getUrl();
                    }
                    AnalyticsHelper.getInstance(getActivity()).sendActionEvent(getClass().getSimpleName(), CONSULTE_PAGE_MOODLE);
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
                return true;
            }
        });
        super.onRequestSuccess(null);
    }
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) MoodleModuleContent(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleModuleContent) ExpandableListMoodleSectionAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.ExpandableListMoodleSectionAdapter) ExpandableListView(android.widget.ExpandableListView) MoodleCoreModule(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreModule) MoodleCoreCourse(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourse) MoodleCoreCourses(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourses) SpringAndroidSpiceRequest(com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest) Intent(android.content.Intent) View(android.view.View) ExpandableListView(android.widget.ExpandableListView)

Example 14 with MimeTypeMap

use of android.webkit.MimeTypeMap in project wire-android by wireapp.

the class AssetUtils method assetMimeTypeToExtension.

public static String assetMimeTypeToExtension(String mimeType) {
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
    if (extension == null) {
        return mimeType;
    }
    return extension;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 15 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)19 Intent (android.content.Intent)5 File (java.io.File)5 DownloadManager (android.app.DownloadManager)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 IntentFilter (android.content.IntentFilter)2 ResolveInfo (android.content.pm.ResolveInfo)2 View (android.view.View)2 ArrayList (java.util.ArrayList)2 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 PackageManager (android.content.pm.PackageManager)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 AdapterView (android.widget.AdapterView)1 ExpandableListView (android.widget.ExpandableListView)1 ListView (android.widget.ListView)1 MoodleCoreCourse (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourse)1 MoodleCoreCourses (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourses)1