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.business.rushour.businessapp.RiderMainActivity.java

/**
 * Display image from a path to ImageView
 *///from w w  w  . j a va 2s .c  o  m
private void previewCapturedImage() {
    try {
        // hide video preview

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(fileUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

From source file:dk.microting.softkeyboard.autoupdateapk.AutoUpdateApk.java

private void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        // raise notification
        Notification notification = new Notification(appIcon, appName + " update", System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);/*from  ww  w . j  a  v a  2 s  .  co  m*/
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        nm.notify(NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }
}

From source file:com.doplgangr.secrecy.views.FileViewer.java

void decrypt(final EncryptedFile encryptedFile, final Runnable onFinish) {
    new AsyncTask<EncryptedFile, Void, File>() {
        @Override//w w w .j a  v  a  2 s.  c  om
        protected File doInBackground(EncryptedFile... encryptedFiles) {
            return getFile(encryptedFile, onFinish);
        }

        @Override
        protected void onPostExecute(File tempFile) {
            if (tempFile != null) {
                if (tempFile.getParentFile().equals(Storage.getTempFolder())) {
                    tempFile = new File(Storage.getTempFolder(), tempFile.getName());
                }
                Uri uri = OurFileProvider.getUriForFile(context, OurFileProvider.FILE_PROVIDER_AUTHORITY,
                        tempFile);
                MimeTypeMap myMime = MimeTypeMap.getSingleton();
                Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
                String mimeType = myMime.getMimeTypeFromExtension(encryptedFile.getType());
                newIntent.setDataAndType(uri, mimeType);
                newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                //altIntent: resort to using file provider when content provider does not work.
                Intent altIntent = new Intent(android.content.Intent.ACTION_VIEW);
                Uri rawuri = Uri.fromFile(tempFile);
                altIntent.setDataAndType(rawuri, mimeType);
                afterDecrypt(newIntent, altIntent);
            }
        }
    }.execute(encryptedFile);
}

From source file:com.microsoft.filediscovery.FileItemActivity.java

protected void openFile(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)), "MIME-TYPE");
    startActivity(install);//from  w w  w. ja  va2  s .  com
}

From source file:com.grass.caishi.cc.activity.RegisterActivity.java

/**
 * ?// w w w. java 2s. c o m
 * 
 * @param uri
 * @param outputX
 * @param outputY
 * @param requestCode
 */
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {

    /*
     * Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); //aspectX aspectY intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY ? intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode);
     */

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", outputX);
    intent.putExtra("outputY", outputY);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true); // no face detection
    startActivityForResult(intent, requestCode);
}

From source file:com.ayuget.redface.ui.misc.ImageMenuHandler.java

/**
 * Saves image from network using OkHttp. Picasso is not used because it would strip away the
 * EXIF data once the image is saved (Picasso directly gives us a Bitmap).
 *///  w  w w  . j a  va  2s . co  m
private void saveImageFromNetwork(final File mediaFile, final Bitmap.CompressFormat targetFormat,
        final boolean compressAsPng, final boolean notifyUser, final boolean broadcastSave,
        final ImageSavedCallback imageSavedCallback) {
    OkHttpClient okHttpClient = new OkHttpClient();

    final Request request = new Request.Builder().url(imageUrl).build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            SnackbarHelper.makeError(activity, R.string.error_saving_image).show();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            final byte[] imageBytes = response.body().bytes();

            Timber.d("Image successfully decoded, requesting WRITE_EXTERNAL_STORAGE permission to save image");

            RxPermissions.getInstance(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean granted) {
                            if (granted) {
                                Timber.d("WRITE_EXTERNAL_STORAGE granted, saving image to disk");

                                try {
                                    Timber.d("Saving image to %s", mediaFile.getAbsolutePath());

                                    if (compressAsPng) {
                                        Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0,
                                                imageBytes.length);
                                        StorageHelper.storeImageToFile(bitmap, mediaFile, targetFormat);
                                    } else {
                                        StorageHelper.storeImageToFile(imageBytes, mediaFile);
                                    }

                                    if (broadcastSave) {
                                        // First, notify the system that a new image has been saved
                                        // to external storage. This is important for user experience
                                        // because it makes the image visible in the system gallery
                                        // app.
                                        StorageHelper.broadcastImageWasSaved(activity, mediaFile, targetFormat);
                                    }

                                    if (notifyUser) {
                                        // Then, notify the user with an enhanced snackbar, allowing
                                        // him (or her) to open the image in his favorite app.
                                        Snackbar snackbar = SnackbarHelper.makeWithAction(activity,
                                                R.string.image_saved_successfully,
                                                R.string.action_snackbar_open_image,
                                                new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent();
                                                        intent.setAction(Intent.ACTION_VIEW);
                                                        intent.setDataAndType(
                                                                Uri.parse("file://"
                                                                        + mediaFile.getAbsolutePath()),
                                                                "image/*");
                                                        activity.startActivity(intent);
                                                    }
                                                });
                                        snackbar.show();
                                    }

                                    notifyImageWasSaved(imageSavedCallback, mediaFile, targetFormat);
                                } catch (IOException e) {
                                    Timber.e(e, "Unable to save image to external storage");
                                    SnackbarHelper.makeError(activity, R.string.error_saving_image).show();
                                }
                            } else {
                                Timber.w("WRITE_EXTERNAL_STORAGE denied, unable to save image");
                                SnackbarHelper
                                        .makeError(activity, R.string.error_saving_image_permission_denied)
                                        .show();
                            }
                        }
                    });
        }
    });
}

From source file:com.example.android.supportv7.media.SampleMediaRouterActivity.java

private Intent makePlayIntent(MediaItem item) {
    Intent intent = new Intent(MediaControlIntent.ACTION_PLAY);
    intent.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK);
    intent.setDataAndType(item.mUri, "video/mp4");
    return intent;
}

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

private void downloadDone() {
    stopDownload();//from w  w w . java2s . c o m
    Intent installIntent = new Intent(Intent.ACTION_VIEW);
    String filename = mTmpFile.getAbsolutePath();
    Uri path = Uri.parse(filename);
    if (path.getScheme() == null) {
        path = Uri.fromFile(new File(filename));
    }
    installIntent.setDataAndType(path, mimetype);
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        getContext().startActivity(installIntent);
    } catch (Exception e) {
        e.printStackTrace();
        mProgressHandler.sendMessage(mProgressHandler.obtainMessage(-2, "?"));
    }
}

From source file:com.android.settings.users.EditUserPhotoController.java

private void cropPhoto(Uri pictureUri) {
    // TODO: Use a public intent, when there is one.
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(pictureUri, "image/*");
    appendOutputExtra(intent, mCropPictureUri);
    appendCropExtras(intent);// ww w .j a va2  s.  c o m
    if (intent.resolveActivity(mContext.getPackageManager()) != null) {
        try {
            StrictMode.disableDeathOnFileUriExposure();
            mFragment.startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO);
        } finally {
            StrictMode.enableDeathOnFileUriExposure();
        }
    } else {
        onPhotoCropped(pictureUri, false);
    }
}

From source file:com.ferid.app.classroom.statistics.StatisticsFragment.java

/**
 * Open and show the created excel file//w  ww.  ja v a 2 s .  c om
 */
private void openExcelFile() {
    if (DirectoryUtility.isExternalStorageMounted()) {

        File file = new File(DirectoryUtility.getPathFolder() + FILE_NAME);

        if (file.exists()) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                //if excel reader not found, ask to download one (Google Sheets)
                Snackbar.make(list, getString(R.string.excelReaderNotFound), Snackbar.LENGTH_LONG)
                        .setAction(getString(R.string.download), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                final String appPackageName = "com.google.android.apps.docs.editors.sheets";
                                try {
                                    startActivity(new Intent(Intent.ACTION_VIEW,
                                            Uri.parse("market://details?id=" + appPackageName)));
                                } catch (android.content.ActivityNotFoundException anfe) {
                                    startActivity(new Intent(Intent.ACTION_VIEW,
                                            Uri.parse("https://play.google.com/store/apps/details?id="
                                                    + appPackageName)));
                                }
                            }
                        }).show();
            }
        } else {
            excelFileError(getString(R.string.excelError));
        }
    } else {
        excelFileError(getString(R.string.mountExternalStorage));
    }
}