Example usage for android.net Uri fromFile

List of usage examples for android.net Uri fromFile

Introduction

In this page you can find the example usage for android.net Uri fromFile.

Prototype

public static Uri fromFile(File file) 

Source Link

Document

Creates a Uri from a file.

Usage

From source file:Main.java

/**
 * call system to install the APK file//from  ww  w  .j ava 2  s.  c  o  m
 * */
public static void installAPKFile(Context context, File file) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    context.startActivity(intent);
}

From source file:Main.java

public static void install(Context context, String apkFilePath) {
    if (context == null) {
        throw new RuntimeException("ApkUtils install apk method and parameter context  == null?");
    }//w  w  w. j av a 2s .c o m

    File file = new File(apkFilePath);

    if (!file.exists()) {
        return;
    }

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    context.startActivity(intent);
}

From source file:Main.java

/**
 * Forces the Android gallery to  refresh its thumbnail images.
 * @param context//from   w w w.  j a  v  a  2 s .  com
 * @param fdelete
 */
private static void refreshGalleryImages(Context context, File fdelete) {
    try {
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    } catch (Exception e1) {
        try {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(fdelete);
            mediaScanIntent.setData(contentUri);
            context.sendBroadcast(mediaScanIntent);
        } catch (Exception e2) {
        }
    }
}

From source file:Main.java

private static void shareImageOnFacebook(String imagePath, Context context) {

    Log.d("CitationsManager-ShareOnFb", "sharing the image " + imagePath);
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    // shareIntent.putExtra(Intent.EXTRA_TEXT, "www.google.com");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {

        Log.d("CitationsManager-ShareOnFb", app.activityInfo.name);
        if ((app.activityInfo.name).contains("com.facebook") && !(app.activityInfo.name).contains("messenger")
                && !(app.activityInfo.name).contains("pages")) {
            final ActivityInfo activity = app.activityInfo;
            final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            shareIntent.setComponent(name);
            context.startActivity(shareIntent);
            break;
        }/*  w  w  w  . j a v a  2  s.com*/
    }

}

From source file:Main.java

public static Intent openFile(File file, String mimeType) {
    return openFile(Uri.fromFile(file), mimeType);
}

From source file:Main.java

public static Intent getOpenFileIntent(File file) {
    if (file == null || !file.exists() || !file.isFile()) {
        return null;
    }//w w w .  j  ava 2 s .c  o  m
    String extension = MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (extension == null || mimeType == null) {
        return null;
    }
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(Uri.fromFile(file), mimeType);
    return intent;
}

From source file:Main.java

public static void disImage(Activity act, File file) {
    Intent itt = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    itt.setData(Uri.fromFile(file));
    act.sendBroadcast(itt);/*from  w w  w. j  av a 2s.  c om*/

    Intent it = new Intent(Intent.ACTION_VIEW);
    it.setDataAndType(Uri.parse(file.getPath()), "image/*");
    act.startActivity(it);
}

From source file:Main.java

static protected Uri getContentURIForPath(String path) {
    return Uri.fromFile(new File(path));
}

From source file:Main.java

public static Intent getEmailIntent(String toEmailAdr, String subject, String message, File attachmentFile,
        String intentChooserTitle) {
    String uriText = "mailto:" + toEmailAdr + "?subject=" + Uri.encode(subject) + "&body="
            + Uri.encode(message);/*  ww  w  .  j ava 2 s. c  o m*/
    Uri uri = Uri.parse(uriText);

    Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
    sendIntent.setData(uri);
    if (attachmentFile != null)
        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
    return Intent.createChooser(sendIntent, intentChooserTitle);
}

From source file:Main.java

/**
 * Get Image Uri when clicked from Camera
 *
 * @return Uri of clicked Image/*from w ww . j  a v a 2  s  .c  o m*/
 */
public static Uri getBitmapUri() {
    File imageStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CRMnextImages");
    if (!imageStorageDir.exists()) {
        imageStorageDir.mkdirs();
    }
    File file = null;
    try {
        file = File.createTempFile("IMG_" + String.valueOf(System.currentTimeMillis()), ".jpg",
                imageStorageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.fromFile(file);
}