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:org.droidpres.activity.TransferActivity.java

private void updateApp() {
    new AlertDialog.Builder(this).setMessage(R.string.msg_UpdateApk)
            .setTitle(android.R.string.dialog_alert_title)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    File apkFile = new File(SetupRootActivity.getApkFileName());
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                    startActivity(intent);
                }/* w w w .j a v  a2  s.c  o m*/
            }).setCancelable(false).show();
}

From source file:de.cortex_media.phonegap.mediaviewer.MediaViewer.java

private void viewMedia(String url, String type) throws IOException {
    Uri uri = Uri.parse(url);//from w w  w . j  ava 2  s. c o m

    Intent intent = null;
    if (url.contains(ASSETS)) {
        String filepath = url.replace(ASSETS, "");
        String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());

        File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename);
        if (!fp.exists()) {
            this.copy(filepath, filename);
        }
        uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename);

        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, type);

        // use for type:
        // "video/*"
        // application/pdf

    } else {
        // Display video player
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, type);
    }

    this.cordova.getActivity().startActivity(intent);
}

From source file:mobisocial.musubi.ui.fragments.FeedViewFragment.java

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_feed_details:
        Intent intent = new Intent(getActivity(), FeedDetailsActivity.class);
        intent.setDataAndType(mFeedUri, MusubiContentProvider.getType(Provided.FEEDS_ID));
        startActivity(intent);/*from w  ww  .  j a v a  2 s .  c  o m*/
        return true;
    }

    return super.onOptionsItemSelected(item);
}

From source file:com.George.iexpense.activity.Echo.java

private PluginResult openFile(String fileUrl) {
    Log.d("FileViewerPlugin", "View file" + fileUrl);
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(fileUrl);
    String extension = fileUrl.substring(fileUrl.lastIndexOf(".") + 1);
    String type = "";
    if (extension.toLowerCase().equals("pdf")) {
        type = "application/pdf";
    } else if (extension.toLowerCase().equals("flv")) {
        type = "video/flv";
    } else if (extension.toLowerCase().equals("mp4")) {
        type = "video/mp4";
    } else if (extension.toLowerCase().equals("jpg")) {
        type = "image/jpg";
    }/*  w  ww .  ja v  a 2  s  .c  om*/
    intent.setDataAndType(Uri.fromFile(file), type);
    cordova.getActivity().startActivity(intent);
    Log.d("FileViewerPlugin", "View complete in" + fileUrl);
    return new PluginResult(PluginResult.Status.OK, fileUrl);
}

From source file:amigoinn.example.v4sales.AutoUpdateApk.java

protected 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) {
        setChanged();//from w ww. j a v a2 s.  c o m
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise the notification
        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);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(appIcon);
        builder.setTicker(appName + " update");
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setContentIntent(contentIntent);
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        builder.setOngoing(true);

        nm.notify(NOTIFICATION_ID, builder.build());
    } else {
        //nm.cancel( NOTIFICATION_ID );   // tried this, but it just doesn't do the trick =(
        nm.cancelAll();
    }
}

From source file:com.example.lista3new.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    mFileDownloadedReceiver = new DownloadReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(mFileDownloadedReceiver,
            new IntentFilter(SyncService.MESSAGE_UPDATE));
    mFileListView = (ListView) findViewById(R.id.fileList);
    mCurrentDirectoryInfo = (TextView) findViewById(R.id.currentPath);
    mLoginInfo = (TextView) findViewById(R.id.loginInfo);
    mFileListView.setOnItemClickListener(new OnItemClickListener() {

        @Override//  w  w  w. j  a v a  2 s  .  c o  m
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            File file = new File(currentPath + "/" + mFileListView.getItemAtPosition(position).toString());
            Log.d(TAG, "OnClick path: " + file.getPath());
            if (file.isDirectory()) {
                currentPath = file;
                loadFiles();
            } else {
                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                String ext = file.getName().substring(file.getName().indexOf(".") + 1);
                String type = mime.getMimeTypeFromExtension(ext);
                intent.setDataAndType(Uri.fromFile(file), type);
                startActivity(intent);
            }
        }
    });
    loadSettings();
    loadFiles();
    Log.i(TAG, "onCreate");
}

From source file:com.android.music.AlbumBrowserFragment.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
    intent.setClass(getActivity(), TrackBrowserActivity.class);
    intent.putExtra("album", Long.valueOf(id).toString());
    intent.putExtra("artist", mArtistId);
    startActivity(intent);// w  w w .  jav a 2 s .c o  m

}

From source file:com.cerema.cloud2.files.FileOperationsHelper.java

public void openFile(OCFile file) {
    if (file != null) {
        String storagePath = file.getStoragePath();
        String encodedStoragePath = WebdavUtils.encodePath(storagePath);

        Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
        intentForSavedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath), file.getMimetype());
        intentForSavedMimeType// w  w  w  .  j  ava2s.c om
                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        Intent intentForGuessedMimeType = null;
        if (storagePath.lastIndexOf('.') >= 0) {
            String guessedMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
            if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
                intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
                intentForGuessedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath),
                        guessedMimeType);
                intentForGuessedMimeType.setFlags(
                        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        Intent openFileWithIntent;
        if (intentForGuessedMimeType != null) {
            openFileWithIntent = intentForGuessedMimeType;
        } else {
            openFileWithIntent = intentForSavedMimeType;
        }

        List<ResolveInfo> launchables = mFileActivity.getPackageManager()
                .queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);

        if (launchables != null && launchables.size() > 0) {
            try {
                mFileActivity.startActivity(Intent.createChooser(openFileWithIntent,
                        mFileActivity.getString(R.string.actionbar_open_with)));
            } catch (ActivityNotFoundException anfe) {
                showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
            }
        } else {
            showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
        }

    } else {
        Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
    }
}

From source file:com.openerp.base.ir.Attachment.java

private void requestIntent(Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    FileNameMap mime = URLConnection.getFileNameMap();
    String mimeType = mime.getContentTypeFor(uri.getPath());
    intent.setDataAndType(uri, mimeType);
    mContext.startActivity(intent);//from   w  w  w  .  j a v a  2s .  c  o  m
}

From source file:com.phonegap.cordova.FileOpener.java

private void openFile(String url) throws IOException {
    // Create URI
    Uri uri = Uri.parse(url);// w w  w.  jav a 2  s. co m

    Intent intent = null;
    // 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.contains(".doc") || url.contains(".docx")) {
        // Word document
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/msword");
    } else if (url.contains(".pdf")) {
        // PDF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/pdf");
    } else if (url.contains(".ppt") || url.contains(".pptx")) {
        // Powerpoint file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if (url.contains(".xls") || url.contains(".xlsx")) {
        // Excel file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if (url.contains(".rtf")) {
        // RTF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/rtf");
    } else if (url.contains(".wav")) {
        // WAV audio file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "audio/x-wav");
    } else if (url.contains(".gif")) {
        // GIF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/gif");
    } else if (url.contains(".jpg") || url.contains(".jpeg")) {
        // JPG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/jpeg");
    } else if (url.contains(".png")) {
        // PNG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/png");
    } else if (url.contains(".txt")) {
        // Text file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/plain");
    } else if (url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4")
            || url.contains(".avi")) {
        // Video files
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    }

    //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

    else {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "*/*");
    }

    this.cordova.getActivity().startActivity(intent);
}