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.theelix.libreexplorer.FileManager.java

/**
 * This method chooses the appropriate Apps and Open the file
 *
 * @param file The target file//from   www.j a v a  2  s. co m
 */
public static void openFile(File file) {
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        String mimeType = FileUtilties.getMimeType(uri);
        if (mimeType == null) {
            mimeType = "*/*";
        }
        intent.setDataAndType(uri, mimeType);
        mContext.startActivity(Intent.createChooser(intent, null));
    } catch (ActivityNotFoundException e) {
        //Activity is not found so App'll start an intent with generic Mimetype
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "*/*");
        mContext.startActivity(Intent.createChooser(intent, null));
    }

}

From source file:com.frostwire.android.gui.util.UIUtils.java

/**
 * Opens the given file with the default Android activity for that File and
 * mime type./* w w  w .  ja  v a2 s  .  c om*/
 */
public static void openFile(Context context, String filePath, String mime, boolean useFileProvider) {
    try {
        if (filePath != null && !openAudioInternal(context, filePath)) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(getFileUri(context, filePath, useFileProvider), Intent.normalizeMimeType(mime));
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (mime != null && mime.contains("video")) {
                if (MusicUtils.isPlaying()) {
                    MusicUtils.playOrPause();
                }
                UXStats.instance().log(UXAction.LIBRARY_VIDEO_PLAY);
            }
            context.startActivity(i);
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.cant_open_file);
        LOG.error("Failed to open file: " + filePath, e);
    }
}

From source file:com.docd.purefm.utils.PFMFileUtils.java

public static void openFileInExternalApp(@NonNull final Context context, @NonNull final File target) {
    final String mime = MimeTypes.getMimeType(target);
    if (mime != null) {
        final Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.fromFile(target), mime);
        final PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            throw new IllegalArgumentException("No PackageManager for context");
        }/*  www  . j  a  v  a2 s  .  c o m*/
        if (packageManager.queryIntentActivities(i, 0).isEmpty()) {
            Toast.makeText(context, R.string.no_apps_to_open, Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            context.startActivity(i);
        } catch (Exception e) {
            Toast.makeText(context, context.getString(R.string.could_not_open_file_) + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

From source file:br.org.funcate.dynamicforms.markers.MarkersUtilities.java

/**
 * Launches Marker in edit mode on a given image, saving over the same image.
 *
 * @param context the context to use.//from  w w  w .  j a  v  a2 s  . co  m
 * @param image   the image to edit and save to.
 */
public static void launchOnImage(final Context context, File image) {
    if (MarkersUtilities.appInstalled(context)) {
        Intent sketchIntent = null;
        if (MARKERS_IS_INTEGRATED) {
            try {
                sketchIntent = new Intent(context, Class.forName(APP_MAIN_ACTIVITY));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            sketchIntent = new Intent();
        }
        sketchIntent.setAction(ACTION_EDIT);
        sketchIntent.setDataAndType(Uri.fromFile(image), "image/*"); //$NON-NLS-1$
        sketchIntent.putExtra(MarkersUtilities.EXTRA_KEY, image.getAbsolutePath());
        context.startActivity(sketchIntent);
    } else {
        MarkersUtilities.openMarketToInstall(context);
    }
}

From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java

public static void launchExternalViewer(Activity activity, String fileUrl) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (!TextUtils.isEmpty(type)) {
        String filePath = fileUrl;
        if (filePath.startsWith("file:")) {
            filePath = filePath.substring("file:".length());
        }//from w ww . j ava  2  s  .com
        Uri photoUri = FileProvider.getUriForFile(activity, activity.getPackageName(), new File(filePath));
        intent.setDataAndType(photoUri, type);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Needed to avoid security exception on KitKat.
            intent.setClipData(ClipData.newRawUri(null, photoUri));
        }
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            activity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "No activity found to handle this " + fileUrl + " type " + type);
        }
    } else {
        Log.w(TAG, "Could not find mime type for " + fileUrl);
    }
}

From source file:com.googlecode.android_scripting.rpc.MethodDescriptor.java

public static Object buildIntent(JSONObject jsonObject) throws JSONException {
    Intent intent = new Intent();
    if (jsonObject.has("action")) {
        intent.setAction(jsonObject.getString("action"));
    }/*w ww  .  ja va 2  s.  com*/
    if (jsonObject.has("data") && jsonObject.has("type")) {
        intent.setDataAndType(Uri.parse(jsonObject.optString("data", null)),
                jsonObject.optString("type", null));
    } else if (jsonObject.has("data")) {
        intent.setData(Uri.parse(jsonObject.optString("data", null)));
    } else if (jsonObject.has("type")) {
        intent.setType(jsonObject.optString("type", null));
    }
    if (jsonObject.has("packagename") && jsonObject.has("classname")) {
        intent.setClassName(jsonObject.getString("packagename"), jsonObject.getString("classname"));
    }
    if (jsonObject.has("flags")) {
        intent.setFlags(jsonObject.getInt("flags"));
    }
    if (!jsonObject.isNull("extras")) {
        AndroidFacade.putExtrasFromJsonObject(jsonObject.getJSONObject("extras"), intent);
    }
    if (!jsonObject.isNull("categories")) {
        JSONArray categories = jsonObject.getJSONArray("categories");
        for (int i = 0; i < categories.length(); i++) {
            intent.addCategory(categories.getString(i));
        }
    }
    return intent;
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java

private static void showMedia(final String localId, final Context ctx, final ProgressDialog dialog) {
    new AsyncTask<Void, Void, Boolean>() {

        private boolean noMedia = false;

        @Override/*from  w w  w .  ja va2 s. c  om*/
        protected Boolean doInBackground(Void... params) {
            File file1 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_JPG);
            File file2 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_3GP);

            Intent mediaPlayer = new Intent(Intent.ACTION_VIEW);

            if (file1.exists()) {
                mediaPlayer.setDataAndType(Uri.fromFile(file1), "image/*");
            } else if (file2.exists()) {
                mediaPlayer.setDataAndType(Uri.fromFile(file2), "video/*");

            } else {
                noMedia = true;
                return false;
            }
            ctx.startActivity(mediaPlayer);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (dialog != null) {
                dialog.dismiss();
            }
            if (noMedia) {
                Toast.makeText(ctx.getApplicationContext(),
                        ctx.getApplicationContext().getText(R.string.no_media), Toast.LENGTH_SHORT).show();
                noMedia = false;
            }
        }

    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static void installApp(Context context, String inAppPath) {
    if (null == inAppPath || 0 == inAppPath.trim().length()) {
        return;/*from   ww w  . j  a va 2 s  .com*/
    }
    String reallyPath = "";
    File file = new File(inAppPath);
    if (file.exists()) {
        reallyPath = inAppPath;
    } else {
        reallyPath = copyFileToStorage(context, inAppPath);
        if (null == reallyPath) {
            return;
        }
    }
    // install apk.
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MimeTypeMap type = MimeTypeMap.getSingleton();
    String mime = type.getMimeTypeFromExtension("apk");
    reallyPath = reallyPath.contains("file://") ? reallyPath : ("file://" + reallyPath);
    intent.setDataAndType(Uri.parse(reallyPath), mime);
    context.startActivity(intent);
}

From source file:com.king.base.util.SystemUtils.java

/**
 * apk//from  ww w  .j av  a  2s  .  c o  m
 * @param context
 * @param file
 */
public static void installApk(Context context, File file) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uriData = null;
    String type = "application/vnd.android.package-archive";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        uriData = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
    } else {
        uriData = Uri.fromFile(file);
    }
    intent.setDataAndType(uriData, type);
    context.startActivity(intent);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static boolean installApk(Context context, String apkPath) {
    File apkFile = new File(apkPath);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");

    try {/* w  ww . j  a v  a 2  s .com*/
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}