Example usage for android.content Intent setDataAndType

List of usage examples for android.content Intent setDataAndType

Introduction

In this page you can find the example usage for android.content Intent setDataAndType.

Prototype

public @NonNull Intent setDataAndType(@Nullable Uri data, @Nullable String type) 

Source Link

Document

(Usually optional) Set the data for the intent along with an explicit MIME data type.

Usage

From source file:com.stroke.academy.common.updater.ApkDownloadUtil.java

public static void installBundledApps(File file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    AcademyApplication.getInstance().startActivity(intent);
}

From source file:net.reichholf.dreamdroid.intents.IntentFactory.java

public static Intent getStreamFileIntent(String fileName, String title) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    SimpleHttpClient shc = SimpleHttpClient.getInstance();
    ArrayList<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("file", fileName));
    Uri uri = Uri.parse(shc.buildFileStreamUrl(URIStore.FILE, params));
    Log.i(DreamDroid.LOG_TAG, "Streaming file: " + uri.getEncodedQuery());

    intent.setDataAndType(uri, "video/*");
    intent.putExtra("title", title);
    return intent;
}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

public static void openFile(final Context context, final File target) {
    final String mime = MimeTypes.getMimeType(target);
    final Intent i = new Intent(Intent.ACTION_VIEW);

    if (mime != null) {
        i.setDataAndType(Uri.fromFile(target), mime);
    } else {//  w ww  .ja  v a2 s  . c om
        i.setDataAndType(Uri.fromFile(target), "*/*");
    }

    if (context.getPackageManager().queryIntentActivities(i, 0).isEmpty()) {
        Toast.makeText(context, R.string.cantopenfile, Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        context.startActivity(i);
    } catch (Exception e) {
        Toast.makeText(context, context.getString(R.string.cantopenfile) + e.getMessage(), Toast.LENGTH_SHORT)
                .show();
    }
}

From source file:com.bt.download.android.gui.util.UIUtils.java

/**
 * Opens the given file with the default Android activity for that File and
 * mime type.// ww  w  . j av  a 2 s.com
 * 
 * @param filePath
 * @param mime
 */
public static void openFile(Context context, String filePath, String mime) {
    try {
        if (!openAudioInternal(filePath)) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(new File(filePath)), mime);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);

            if (mime != null && mime.contains("video")) {
                if (MusicUtils.isPlaying()) {
                    MusicUtils.playOrPause();
                }
                UXStats.instance().log(UXAction.LIBRARY_VIDEO_PLAY);
            }
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.cant_open_file);
        Log.e(TAG, "Failed to open file: " + filePath, e);
    }
}

From source file:com.mediatek.mms.util.VCardUtils.java

public static void importVCard(Context context, FileAttachmentModel attach) {
    MessageUtils.deleteAllSharedFiles(context, ".vcf");
    MmsLog.d(TAG, "importVCard, file uri: " + attach.getUri());
    final File tempVCard = MessageUtils.createTempFileExposed(context, attach.getUri(), attach.getSrc());
    if (tempVCard == null || !tempVCard.exists() || tempVCard.length() <= 0) {
        MmsLog.e(TAG, "importVCard, file is not exists or empty " + tempVCard);
        return;//from   w  w w. j  a v  a  2  s  . c om
    }

    Uri vCardUri = FileProvider.getUriForFile(context, MMS_FILE_PROVIDER_AUTHORITIES, tempVCard);
    MmsLog.i(TAG, "importVCard, vCard uri: " + vCardUri);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(vCardUri, attach.getContentType().toLowerCase());
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(intent);
}

From source file:Main.java

public static void openFile(Context context, File url) throws IOException {
    // Create URI
    File file = url;//from ww w .  jav  a  2 s . co m
    Uri uri = Uri.fromFile(file);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type, 
    // so Android knew what application to use to open the file
    if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
        // Word document
        intent.setDataAndType(uri, "application/msword");
    } else if (url.toString().contains(".pdf")) {
        // PDF file
        intent.setDataAndType(uri, "application/pdf");
    } else if (url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
        // Powerpoint file
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if (url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
        // Excel file
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if (url.toString().contains(".zip") || url.toString().contains(".rar")) {
        // WAV audio file
        intent.setDataAndType(uri, "application/zip");
    } else if (url.toString().contains(".rtf")) {
        // RTF file
        intent.setDataAndType(uri, "application/rtf");
    } else if (url.toString().contains(".wav") || url.toString().contains(".mp3")) {
        // WAV audio file
        intent.setDataAndType(uri, "audio/x-wav");
    } else if (url.toString().contains(".gif")) {
        // GIF file
        intent.setDataAndType(uri, "image/gif");
    } else if (url.toString().contains(".jpg") || url.toString().contains(".jpeg")
            || url.toString().contains(".png")) {
        // JPG file
        intent.setDataAndType(uri, "image/jpeg");
    } else if (url.toString().contains(".txt")) {
        // Text file
        intent.setDataAndType(uri, "text/plain");
    } else if (url.toString().contains(".3gp") || url.toString().contains(".mpg")
            || url.toString().contains(".mpeg") || url.toString().contains(".mpe")
            || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
        // Video files
        intent.setDataAndType(uri, "video/*");
    } else {
        //if you want you can also define the intent type for any other file

        //additionally use else clause below, to manage other unknown extensions
        //in this case, Android will show all applications installed on the device
        //so you can choose which application to use
        intent.setDataAndType(uri, "*/*");
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

From source file:Main.java

public static void openFileInExtApp(Context context, File url) throws IOException {
    // Create URI
    File file = url;/*from  ww w  .  jav  a 2  s .  co m*/
    Uri uri = Uri.fromFile(file);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type,
    // so Android knew what application to use to open the file
    if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
        // Word document
        intent.setDataAndType(uri, "application/msword");
    } else if (url.toString().contains(".pdf")) {
        // PDF file
        intent.setDataAndType(uri, "application/pdf");
    } else if (url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
        // Powerpoint file
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if (url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
        // Excel file
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if (url.toString().contains(".zip") || url.toString().contains(".rar")) {
        // WAV audio file
        intent.setDataAndType(uri, "application/x-wav");
    } else if (url.toString().contains(".rtf")) {
        // RTF file
        intent.setDataAndType(uri, "application/rtf");
    } else if (url.toString().contains(".wav") || url.toString().contains(".mp3")) {
        // WAV audio file
        intent.setDataAndType(uri, "audio/x-wav");
    } else if (url.toString().contains(".gif")) {
        // GIF file
        intent.setDataAndType(uri, "image/gif");
    } else if (url.toString().contains(".jpg") || url.toString().contains(".jpeg")
            || url.toString().contains(".png")) {
        // JPG file
        intent.setDataAndType(uri, "image/jpeg");
    } else if (url.toString().contains(".txt")) {
        // Text file
        intent.setDataAndType(uri, "text/plain");
    } else if (url.toString().contains(".html")) {
        // Html file
        intent.setDataAndType(uri, "text/html");
    } else if (url.toString().contains(".3gp") || url.toString().contains(".mpg")
            || url.toString().contains(".mpeg") || url.toString().contains(".mpe")
            || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
        // Video files
        intent.setDataAndType(uri, "video/*");
    } else {
        //if you want you can also define the intent type for any other file

        //additionally use else clause below, to manage other unknown extensions
        //in this case, Android will show all applications installed on the device
        //so you can choose which application to use
        intent.setDataAndType(uri, "*/*");
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

From source file:edu.mit.mobile.android.locast.data.CastMedia.java

/**
 * @param context//from ww w .ja v a 2s  . c  om
 * @param c
 * @param castMediaUri
 *
 */
public static void showMedia(Context context, Cursor c, Uri castMediaUri) {
    final String mediaString = c.getString(c.getColumnIndex(CastMedia._MEDIA_URL));
    final String locMediaString = c.getString(c.getColumnIndex(CastMedia._LOCAL_URI));
    String mimeType = null;

    Uri media;

    if (locMediaString != null) {
        media = Uri.parse(locMediaString);
        if ("file".equals(media.getScheme())) {
            mimeType = c.getString(c.getColumnIndex(CastMedia._MIME_TYPE));
        }

    } else if (mediaString != null) {
        media = Uri.parse(mediaString);
        mimeType = c.getString(c.getColumnIndex(CastMedia._MIME_TYPE));

        // we strip this because we don't really want to force them to go to the browser.
        if ("text/html".equals(mimeType)) {
            mimeType = null;
        }
    } else {
        Log.e(TAG, "asked to show media for " + castMediaUri + " but there was nothing to show");
        return;
    }

    final Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(media, mimeType);

    if (mimeType != null && mimeType.startsWith("video/")) {
        context.startActivity(new Intent(Intent.ACTION_VIEW,
                ContentUris.withAppendedId(castMediaUri, c.getLong(c.getColumnIndex(CastMedia._ID)))));
    } else {
        // setting the MIME type for URLs doesn't work.
        try {
            context.startActivity(i);
        } catch (final ActivityNotFoundException e) {
            // try it again, but without a mime type.
            if (mimeType != null) {
                i.setDataAndType(media, null);
            }
            try {
                context.startActivity(i);
            } catch (final ActivityNotFoundException e2) {
                Toast.makeText(context, R.string.error_cast_media_no_activities, Toast.LENGTH_LONG).show();
            }
        }
    }
}

From source file:mobisocial.musubi.objects.FileObj.java

public static void viewfile(Context context, Uri contentUri, String mimetype) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (mimetype == null || mimetype.isEmpty()) {
        return;//w  w  w  . j  a  va  2s.c  om
    }
    try {
        intent.setDataAndType(contentUri, mimetype);
        context.startActivity(intent);
    } catch (ActivityNotFoundException anfe) {
        // TODO redirect Google Play to download viewer app for each filetype
        return;
    }
}

From source file:com.dv.Utils.Tools.java

/**
 * install app/* w ww . j  ava2  s .co  m*/
 *
 * @param context
 * @param filePath
 * @return whether apk exist
 */
public static boolean install(Context context, String filePath) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    File file = new File(filePath);

    if ((file != null) && (file.length() > 0) && file.exists() && file.isFile()) {
        i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

        return true;
    }

    return false;
}