List of usage examples for android.content Intent setDataAndType
public @NonNull Intent setDataAndType(@Nullable Uri data, @Nullable String type)
From source file:com.apptentive.android.sdk.util.Util.java
/** * This function launchs the default app to view the selected file, based on mime type * * @param sourcePath/* w w w . j ava 2s .c o m*/ * @param selectedFilePath the full path to the local storage * @param mimeTypeString the mime type of the file to be opened * @return true if file can be viewed */ public static boolean openFileAttachment(final Context context, final String sourcePath, final String selectedFilePath, final String mimeTypeString) { if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) && hasPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { File selectedFile = new File(selectedFilePath); String selectedFileName = null; if (selectedFile.exists()) { selectedFileName = selectedFile.getName(); final Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); /* Attachments were downloaded into app private data dir. In order for external app to open * the attachments, the file need to be copied to a download folder that is accessible to public * The folder will be sdcard/Downloads/apptentive-received/<file name> */ File downloadFolder = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File apptentiveSubFolder = new File(downloadFolder, "apptentive-received"); if (!apptentiveSubFolder.exists()) { apptentiveSubFolder.mkdir(); } File tmpfile = new File(apptentiveSubFolder, selectedFileName); String tmpFilePath = tmpfile.getPath(); // If destination file already exists, overwrite it; otherwise, delete all existing files in the same folder first. if (!tmpfile.exists()) { String[] children = apptentiveSubFolder.list(); if (children != null) { for (int i = 0; i < children.length; i++) { new File(apptentiveSubFolder, children[i]).delete(); } } } if (copyFile(selectedFilePath, tmpFilePath) == 0) { return false; } intent.setDataAndType(Uri.fromFile(tmpfile), mimeTypeString); try { context.startActivity(intent); return true; } catch (ActivityNotFoundException e) { ApptentiveLog.e("Activity not found to open attachment: ", e); } } } else { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourcePath)); if (Util.canLaunchIntent(context, browserIntent)) { context.startActivity(browserIntent); } } return false; }
From source file:com.amaze.carbonfilemanager.utils.files.Futils.java
public void openFile(final File f, final MainActivity m) { boolean defaultHandler = isSelfDefault(f, m); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(m); if (defaultHandler && f.getName().toLowerCase().endsWith(".zip") || f.getName().toLowerCase().endsWith(".jar") || f.getName().toLowerCase().endsWith(".rar") || f.getName().toLowerCase().endsWith(".tar") || f.getName().toLowerCase().endsWith(".tar.gz")) { GeneralDialogCreation.showArchiveDialog(f, m); } else if (f.getName().toLowerCase().endsWith(".apk")) { GeneralDialogCreation.showPackageDialog(f, m); } else if (defaultHandler && f.getName().toLowerCase().endsWith(".db")) { Intent intent = new Intent(m, DbViewer.class); intent.putExtra("path", f.getPath()); m.startActivity(intent);//from w ww .ja v a2 s . co m } else if (Icons.isAudio(f.getPath())) { 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 != null) studioCount.cancel(); studioCount = Toast.makeText(m, sec + "", Toast.LENGTH_LONG); studioCount.show(); } @Override public void onFinish() { if (studioCount != null) studioCount.cancel(); studioCount = Toast.makeText(m, m.getString(R.string.opening), Toast.LENGTH_LONG); studioCount.show(); m.startActivity(intent); } }.start(); } else m.startActivity(intent); } else { try { openunknown(f, m, false); } catch (Exception e) { Toast.makeText(m, m.getResources().getString(R.string.noappfound), Toast.LENGTH_LONG).show(); openWith(f, m); } } }
From source file:com.almarsoft.GroundhogReader.MessageActivity.java
protected void attachClicked(final String attachcode) { HashMap<String, String> tmpattachPart = null; String tmpmd5 = null;//from w ww . ja v a2 s. com for (HashMap<String, String> part : mMimePartsVector) { tmpmd5 = part.get("md5"); if (tmpmd5.equals(attachcode)) { tmpattachPart = part; break; } } final String md5 = tmpmd5; final HashMap<String, String> attachPart = tmpattachPart; if (attachPart != null && md5 != null) { new AlertDialog.Builder(this).setTitle(getString(R.string.attachment)) .setMessage(getString(R.string.open_save_attach_question)) .setPositiveButton(getString(R.string.open), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); File attFile = new File(UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME + "/" + UsenetConstants.ATTACHMENTSDIR + "/" + mGroup, md5); Uri attachUri = Uri.fromFile(attFile); intent.setDataAndType(attachUri, attachPart.get("type")); startActivity(intent); } }).setNegativeButton(getString(R.string.save), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { try { String finalPath = FSUtils.saveAttachment(md5, mGroup, attachPart.get("name")); Toast.makeText(MessageActivity.this, getString(R.string.saved_to) + finalPath, Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(MessageActivity.this, getString(R.string.could_not_save_colon) + e.toString(), Toast.LENGTH_LONG) .show(); } } }).show(); } }
From source file:com.nttec.everychan.ui.gallery.GalleryActivity.java
private void openExternal() { GalleryItemViewTag tag = getCurrentTag(); if (tag == null) return;/*from w w w . ja v a 2 s.co m*/ String mime; switch (tag.attachmentModel.type) { case AttachmentModel.TYPE_VIDEO: mime = "video/*"; break; case AttachmentModel.TYPE_AUDIO: mime = "audio/*"; break; default: mime = "*/*"; break; } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(tag.file), mime); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
From source file:com.igniva.filemanager.utils.Futils.java
private boolean isSelfDefault(File f, Context c) { Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(f), MimeTypes.getMimeType(f)); String s = ""; ResolveInfo rii = c.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (rii != null && rii.activityInfo != null) s = rii.activityInfo.packageName; if (s.equals("com.igniva.filemanager") || rii == null) return true; else// w ww .j a va 2s . co m return false; }
From source file:com.amaze.filemanager.utils.Futils.java
public void openWith(final File f, final Context c) { MaterialDialog.Builder a = new MaterialDialog.Builder(c); a.title(getString(c, R.string.openas)); String[] items = new String[] { getString(c, R.string.text), getString(c, R.string.image), getString(c, R.string.video), getString(c, R.string.audio), getString(c, R.string.database), getString(c, R.string.other) }; a.items(items).itemsCallback(new MaterialDialog.ListCallback() { @Override/*w w w . j a va 2 s . c o m*/ public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence charSequence) { Uri uri = fileToContentUri(c, f); if (uri == null) uri = Uri.fromFile(f); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); switch (i) { case 0: intent.setDataAndType(uri, "text/*"); break; case 1: intent.setDataAndType(uri, "image/*"); break; case 2: intent.setDataAndType(uri, "video/*"); break; case 3: intent.setDataAndType(uri, "audio/*"); break; case 4: intent = new Intent(c, DbViewer.class); intent.putExtra("path", f.getPath()); break; case 5: intent.setDataAndType(uri, "*/*"); break; } try { c.startActivity(intent); } catch (Exception e) { Toast.makeText(c, R.string.noappfound, Toast.LENGTH_SHORT).show(); openWith(f, c); } } }); try { a.build().show(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.arkami.myidkey.activity.KeyCardEditActivity.java
/** * Adds view to linear layout//from w ww . ja va2 s.c o m */ private void addFileToList(final FileObject fileObject) { if (fileObject == null) { return; } if (fileObject.getId() == null) { fileObject.setId(fileDataSource.insert(fileObject)); long[] fileIds = Arrays.copyOf(keyCard.getFileIds(), keyCard.getFileIds().length + 1); fileIds[fileIds.length - 1] = fileObject.getId(); keyCard.setFileIds(fileIds); } // long[] newIds = Arrays.copyOf(keyCard.getFileIds(), // keyCard.getFileIds().length + 1); // long newId = fileObject.getId(); // newIds[newIds.length - 1] = newId; // Toast.makeText(this, newId + "", Toast.LENGTH_SHORT).show(); // keyCard.setFileIds(newIds); // keyCardDataSource.update(keyCard); final View view = View.inflate(this, R.layout.photo_audio_list_item, null); TextView fileName = (TextView) view.findViewById(R.id.photo_audio_list_item_file_name); TextView fileSize = (TextView) view.findViewById(R.id.photo_audio_list_item_file_size); ImageView thumb = (ImageView) view.findViewById(R.id.photo_audio_list_item_thumb); ImageButton close = (ImageButton) view.findViewById(R.id.photo_audio_list_item_close_button); fileName.setText(fileObject.getName()); fileSize.setText(fileObject.getSize() + " bytes"); view.setVisibility(View.VISIBLE); switch (Cache.getInstance(this).getFileTypeEnum(fileObject.getFileType())) { case audio: // add audio close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { audioLinearLayout.removeView(view); if (audioLinearLayout.getChildCount() == 0) { audioListRelativeLayout.setVisibility(View.GONE); } keyCard = removeFileID(fileObject.getId(), keyCard); // KeyCardEditActivity.this.keyCardDataSource.update(keyCard); } }); thumb.setImageResource(R.drawable.speaker); thumb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(fileObject.getPathToFile()); intent.setDataAndType(Uri.fromFile(file), "audio/3gp"); Log.w("Uri.fromFile(file) = ", fileObject.getPathToFile()); startActivity(intent); } }); audioLinearLayout.addView(view); audioListRelativeLayout.setVisibility(View.VISIBLE); break; case photo: // add photo // for photo file close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { photosLinearLayout.removeView(view); if (photosLinearLayout.getChildCount() == 0) { photosListRelativeLayout.setVisibility(View.GONE); } if (fileObject.getId() != null) { keyCard = removeFileID(fileObject.getId(), keyCard); } } }); thumb.setImageBitmap(Common.getImageFromIntent(fileObject.getPathToFile(), true)); thumb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent viewImageIntent = new Intent(KeyCardEditActivity.this, ViewImageIActivity.class); viewImageIntent.putExtra(ViewImageIActivity.IMAGE_KEY, fileObject); startActivity(viewImageIntent); } }); photosLinearLayout.addView(view); photosListRelativeLayout.setVisibility(View.VISIBLE); // end for photo file break; default: break; } }
From source file:com.filemanager.free.utils.Futils.java
private boolean isSelfDefault(File f, Context c) { Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(f), MimeTypes.getMimeType(f)); String s = ""; ResolveInfo rii = c.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (rii != null && rii.activityInfo != null) s = rii.activityInfo.packageName; if (s.equals("com.filemanager.free") || rii == null) return true; else//from w ww.j ava 2s .c o m return false; }
From source file:org.witness.ssc.xfer.utils.PublishingUtils.java
public void launchVideoPlayer(final Activity activity, final String movieurl) { try {/*from www .j ava 2 s. c o m*/ Intent tostart = new Intent(Intent.ACTION_VIEW); tostart.setDataAndType(Uri.parse(movieurl), "video/*"); activity.startActivity(tostart); } catch (android.content.ActivityNotFoundException e) { Log.e(TAG, " Cant start activity to show video!"); new AlertDialog.Builder(activity).setMessage(R.string.cant_show_video) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); return; } }
From source file:com.ferdi2005.secondgram.AndroidUtilities.java
public static void openForView(TLObject media, Activity activity) throws Exception { if (media == null || activity == null) { return;/*w ww . j a v a2 s . co m*/ } String fileName = FileLoader.getAttachFileName(media); File f = FileLoader.getPathToAttach(media, true); if (f != null && f.exists()) { String realMimeType = null; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); MimeTypeMap myMime = MimeTypeMap.getSingleton(); int idx = fileName.lastIndexOf('.'); if (idx != -1) { String ext = fileName.substring(idx + 1); realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase()); if (realMimeType == null) { if (media instanceof TLRPC.TL_document) { realMimeType = ((TLRPC.TL_document) media).mime_type; } if (realMimeType == null || realMimeType.length() == 0) { realMimeType = null; } } } if (Build.VERSION.SDK_INT >= 24) { intent.setDataAndType( FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f), realMimeType != null ? realMimeType : "text/plain"); } else { intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain"); } if (realMimeType != null) { try { activity.startActivityForResult(intent, 500); } catch (Exception e) { if (Build.VERSION.SDK_INT >= 24) { intent.setDataAndType( FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f), "text/plain"); } else { intent.setDataAndType(Uri.fromFile(f), "text/plain"); } activity.startActivityForResult(intent, 500); } } else { activity.startActivityForResult(intent, 500); } } }