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:Main.java

public static void openVideo(Context mContext, String videoPath) {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(videoPath));
    intent.setDataAndType(uri, "video/*");
    mContext.startActivity(intent);/*  w  w w . j a v a2  s  . c  o  m*/
}

From source file:Main.java

public static void installApk(Context context, File apkfile) {
    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setType("application/vnd.android.package-archive");
    intent.setData(Uri.fromFile(apkfile));
    intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);/*from w w w  .ja  va 2  s. c om*/
}

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

public static Intent getStreamServiceIntent(String ref, String title) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String uriString = SimpleHttpClient.getInstance().buildServiceStreamUrl(ref, title);
    Log.i(DreamDroid.LOG_TAG, "Service-Streaming URL set to '" + uriString + "'");

    intent.setDataAndType(Uri.parse(uriString), "video/*");
    intent.putExtra("title", title);
    return intent;
}

From source file:Main.java

public static void installApk(Context context, File file) {
    if (context != null && isFileExists(file)) {
        Uri uri = Uri.fromFile(new File(file.getAbsolutePath()));
        Intent installIntent = new Intent();
        installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        installIntent.setAction(Intent.ACTION_VIEW);
        String type = "application/vnd.android.package-archive";
        installIntent.putExtra("loadapk", "loadapk");
        installIntent.setDataAndType(uri, type);
        context.startActivity(installIntent);
    }/*from  ww w  . ja v a  2s  .c o  m*/
}

From source file:Main.java

public static void openFile(File aFile, Context context) {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(aFile.getAbsolutePath());
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file), mimetype);

    context.startActivity(myIntent);//from   w  w w .j av  a  2 s.  c om
}

From source file:com.bt.heliniumstudentapp.UpdateClass.java

protected static void downloadAPK() {
    File oldUpdate = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS
            + "/heliniumstudentapp.apk");
    if (oldUpdate.exists()) //noinspection ResultOfMethodCallIgnored
        oldUpdate.delete();//from w  ww . java  2 s .  co  m

    if (MainActivity.isOnline()) {
        DownloadManager.Request request;

        request = new DownloadManager.Request(Uri.parse(HeliniumStudentApp.URL_UPDATE_RELEASE));

        request.setTitle(context.getString(R.string.app_name) + " " + versionName);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/heliniumstudentapp.apk");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request);

        context.registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                final Intent install = new Intent(Intent.ACTION_VIEW);
                install.setDataAndType(
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/"
                                + Environment.DIRECTORY_DOWNLOADS + "/heliniumstudentapp.apk")),
                        "application/vnd.android.package-archive");
                context.startActivity(install);
            }
        }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    } else {
        Toast.makeText(context, R.string.error_conn_no, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.android.browser.DownloadHandler.java

/**
 * Notify the host application a download should be done, or that
 * the data should be streamed if a streaming viewer is available.
 * @param activity Activity requesting the download.
 * @param url The full url to the content that should be downloaded
 * @param userAgent User agent of the downloading application.
 * @param contentDisposition Content-disposition http header, if present.
 * @param mimetype The mimetype of the content reported by the server
 * @param referer The referer associated with the downloaded url
 * @param privateBrowsing If the request is coming from a private browsing tab.
 *//*from  www .  j a  v a 2 s .  c o  m*/
public static void onDownloadStart(Activity activity, String url, String userAgent, String contentDisposition,
        String mimetype, String referer, boolean privateBrowsing) {
    // if we're dealing wih A/V content that's not explicitly marked
    //     for download, check if it's streamable.
    if (contentDisposition == null || !contentDisposition.regionMatches(true, 0, "attachment", 0, 10)) {
        // query the package manager to see if there's a registered handler
        //     that matches.
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), mimetype);
        ResolveInfo info = activity.getPackageManager().resolveActivity(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (info != null) {
            ComponentName myName = activity.getComponentName();
            // If we resolved to ourselves, we don't want to attempt to
            // load the url only to try and download it again.
            if (!myName.getPackageName().equals(info.activityInfo.packageName)
                    || !myName.getClassName().equals(info.activityInfo.name)) {
                // someone (other than us) knows how to handle this mime
                // type with this scheme, don't download.
                try {
                    activity.startActivity(intent);
                    return;
                } catch (ActivityNotFoundException ex) {
                    if (LOGD_ENABLED) {
                        Log.d(LOGTAG,
                                "activity not found for " + mimetype + " over " + Uri.parse(url).getScheme(),
                                ex);
                    }
                    // Best behavior is to fall back to a download in this
                    // case
                }
            }
        }
    }
    onDownloadStartNoStream(activity, url, userAgent, contentDisposition, mimetype, referer, privateBrowsing);
}

From source file:com.github.czy1121.update.app.utils.UpdateUtil.java

public static void install(Context context, File file, boolean force) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    } else {/*  w ww  .  j  a  v a  2 s.c  o m*/
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".updatefileprovider", file);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    if (force) {
        System.exit(0);
    }
}

From source file:at.wada811.utils.IntentUtils.java

/**
 * Viewer???Intent??/*from   www  . j  av a2  s .c  o  m*/
 *
 * @param filePath
 * @param mimeType
 * @return intent
 */
public static Intent createFileViewIntent(String filePath) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(filePath)), MediaUtils.getMimeType(filePath));
    return intent;
}

From source file:com.partner.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);
    PartnerApplication.getInstance().startActivity(intent);
}