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.example.zayankovsky.homework.ui.ImageDetailActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.save:
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        } else {/*from   w  w w .  j  ava 2 s.com*/
            saveImageToGallery();
        }
        return true;
    case R.id.browser:
        Uri webpage = Uri.parse(FotkiWorker.getUrl());
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
            return true;
        }
        return false;
    case R.id.open:
        Uri imageUri = getUriForImage();
        if (imageUri == null) {
            return false;
        }

        Intent openIntent = new Intent();
        openIntent.setAction(Intent.ACTION_VIEW);
        // Put the Uri and MIME type in the result Intent
        openIntent.setDataAndType(imageUri, getContentResolver().getType(imageUri));
        // Grant temporary read permission to the content URI
        openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(openIntent);
        return true;
    case R.id.share:
        imageUri = getUriForImage();
        if (imageUri == null) {
            return false;
        }

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        // Put the Uri and MIME type in the result Intent
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType(getContentResolver().getType(imageUri));
        // Grant temporary read permission to the content URI
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, null));
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

From source file:com.anhubo.anhubo.ui.activity.unitDetial.UploadingActivity1.java

/**
 * ?/*from w  ww  .  ja va 2  s.  c om*/
 */
private void startCameraCrop() {
    // ??
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(imageUri, "image/*");
    intent.putExtra("scale", true);
    //
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    //?
    intent.putExtra("outputX", 1080);
    intent.putExtra("outputY", 720);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    //
    Intent intentBc = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intentBc.setData(imageUri);
    this.sendBroadcast(intentBc);
    startActivityForResult(intent, CROP_PHOTO); //??ImageView
}

From source file:com.amaze.filemanager.utils.files.FileUtils.java

public static void openFile(final File f, final MainActivity m, SharedPreferences sharedPrefs) {
    boolean useNewStack = sharedPrefs.getBoolean(PreferencesConstants.PREFERENCE_TEXTEDITOR_NEWSTACK, false);
    boolean defaultHandler = isSelfDefault(f, m);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(m);
    final Toast[] studioCount = { null };

    if (f.getName().toLowerCase().endsWith(".apk")) {
        GeneralDialogCreation.showPackageDialog(f, m);
    } else if (defaultHandler && CompressedHelper.isFileExtractable(f.getPath())) {
        GeneralDialogCreation.showArchiveDialog(f, m);
    } else if (defaultHandler && f.getName().toLowerCase().endsWith(".db")) {
        Intent intent = new Intent(m, DatabaseViewerActivity.class);
        intent.putExtra("path", f.getPath());
        m.startActivity(intent);//from   www .j  av a 2 s  .c o  m
    } else if (Icons.getTypeOfFile(f.getPath(), f.isDirectory()) == Icons.AUDIO) {
        final int studio_count = sharedPreferences.getInt("studio", 0);
        Uri uri = Uri.fromFile(f);
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "audio/*");

        // Behold! It's the  legendary easter egg!
        if (studio_count != 0) {
            new CountDownTimer(studio_count, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int sec = (int) millisUntilFinished / 1000;
                    if (studioCount[0] != null)
                        studioCount[0].cancel();
                    studioCount[0] = Toast.makeText(m, sec + "", Toast.LENGTH_LONG);
                    studioCount[0].show();
                }

                @Override
                public void onFinish() {
                    if (studioCount[0] != null)
                        studioCount[0].cancel();
                    studioCount[0] = Toast.makeText(m, m.getString(R.string.opening), Toast.LENGTH_LONG);
                    studioCount[0].show();
                    m.startActivity(intent);
                }
            }.start();
        } else
            m.startActivity(intent);
    } else {
        try {
            openunknown(f, m, false, useNewStack);
        } catch (Exception e) {
            Toast.makeText(m, m.getString(R.string.noappfound), Toast.LENGTH_LONG).show();
            openWith(f, m, useNewStack);
        }
    }
}

From source file:com.melvin.share.zxing.activity.CaptureActivity.java

/**
 * toolbar??/*from  w  w  w .  j a  va 2s  .co m*/
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    case R.id.camera:
        Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
        // ??"image/jpeg ? image/png"
        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(pickIntent, 10);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.UpdateManager.java

private void installPackage(final Context context, final byte[] apkContents, final UpdateListener listener) {
    final AsyncTask<Void, Void, File> task = new AsyncTask<Void, Void, File>() {
        volatile Exception exception;

        @SuppressLint("WorldReadableFiles")
        @Override/*w w w . j  a va  2s. co m*/
        protected File doInBackground(Void... params) {
            try {
                if (Utilities.isNullOrEmpty(apkContents)) {
                    throw new NullPointerException("apkContents");
                }

                String tempFileName = "nextversion.apk";
                File tempFile = context.getFileStreamPath(tempFileName);
                if (tempFile.exists()) {
                    tempFile.delete();
                }

                FileOutputStream fout = context.openFileOutput(tempFileName, Context.MODE_WORLD_READABLE);
                try {
                    fout.write(apkContents);
                    fout.flush();
                } finally {
                    try {
                        fout.close();
                    } catch (Exception e) {
                        // omit
                    }
                }

                return tempFile;

            } catch (Exception e) {
                exception = e;
                Log.e("Couldn't save apk", e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(File result) {
            if (exception == null) {
                try {
                    Uri data = Uri.fromFile(result);
                    String type = "application/vnd.android.package-archive";
                    Intent promptInstall = new Intent(Intent.ACTION_VIEW);
                    promptInstall.setDataAndType(data, type);
                    context.startActivity(promptInstall);
                    if (listener != null) {
                        listener.onUpdateCompleted();
                    }
                    return;
                } catch (Exception e) {
                    exception = e;
                }
            }

            if (listener == null) {
                Log.e("Update failed", exception);
            } else {
                listener.onUpdateFailed(exception);
            }

        }

    };
    task.execute();
}

From source file:abanoubm.dayra.main.Main.java

private void importDB() {
    Intent intentImport = new Intent(Intent.ACTION_GET_CONTENT);
    intentImport.setDataAndType(Uri.fromFile(new File(Utility.getDayraFolder())), "application/octet-stream");

    if (getApplicationContext().getPackageManager().queryIntentActivities(intentImport, 0).size() > 0) {
        startActivityForResult(intentImport, IMPORT_DB);
    } else {//from  ww  w .  j  a  v a2 s  .  c  om
        Toast.makeText(getApplicationContext(), R.string.err_msg_explorer, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.liwn.zzl.markbit.DrawOptionsMenuActivity.java

private void cropImage(Uri uri) {
    File file = FileIO.getFile(this, uri);
    Uri tmp = Uri.fromFile(file);//  w ww .j  a  v a  2 s.  c o m

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(tmp, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", MarkBitApplication.BIT_LCD_WIDTH);
    intent.putExtra("outputY", MarkBitApplication.BIT_LCD_HEIGHT);
    intent.putExtra("scale", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra("noFaceDetection", true);
    intent.putExtra("return-data", false);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    startActivityForResult(intent, REQUEST_CODE_CROP);
}

From source file:com.phonegap.plugins.discoverscanar.DiscoverScanAR.java

public void arscan(String json_file) {

    Intent intentARScan = new Intent();
    intentARScan.setAction(Intent.ACTION_VIEW);

    if (json_file == null) {
        json_file = "hamilton.json";
    }// w ww.j  a va  2  s . com

    // Used to be hardwired to: "http://www.cs.waikato.ac.nz/~davidb/tipple/uni-mixare-locdata.json"
    intentARScan.setDataAndType(Uri.parse("file:///sdcard/tipple-store/geodata/" + json_file),
            "application/mixare-json");
    this.cordova.startActivityForResult((CordovaPlugin) this, intentARScan, REQUEST_ARSCAN_CODE);
}

From source file:br.com.hotforms.usewaze.UseWaze.java

private void callWaze(final String url) {
    final Activity activity = this.cordova.getActivity();
    activity.runOnUiThread(new Runnable() {
        @Override//  ww  w  .ja va  2 s .c om
        public void run() {
            try {
                Intent intent = null;
                intent = new Intent(Intent.ACTION_VIEW);
                // Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
                // Adding the MIME type to http: URLs causes them to not be handled by the downloader.
                Uri uri = Uri.parse(url);
                if ("file".equals(uri.getScheme())) {
                    intent.setDataAndType(uri, webView.getResourceApi().getMimeType(uri));
                } else {
                    intent.setData(uri);
                }

                activity.startActivity(intent);
            } catch (ActivityNotFoundException ex) {
                String urlMarket = "market://details?id=com.waze";
                Intent intent = null;
                intent = new Intent(Intent.ACTION_VIEW);
                // Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
                // Adding the MIME type to http: URLs causes them to not be handled by the downloader.
                Uri uri = Uri.parse(urlMarket);
                if ("file".equals(uri.getScheme())) {
                    intent.setDataAndType(uri, webView.getResourceApi().getMimeType(uri));
                } else {
                    intent.setData(uri);
                }

                activity.startActivity(intent);
            }
        }
    });
}

From source file:com.feytuo.chat.activity.SettingsFragment.java

private void startPhotoZoom(Uri uri, int size) {
    Log.e("zoom", "begin");
    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", crop);// ?
    intent.putExtra("outputY", crop);
    intent.putExtra("return-data", true);
    Log.e("zoom", "begin1");
    startActivityForResult(intent, PHOTO_REQUEST_CUT);
}