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

public static Intent getCameraIntent(String outputFile) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(outputFile)));
    return intent;
}

From source file:Main.java

public static void installApk(Context context, String filePath) {
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
    context.startActivity(intent);/*from  ww w.  ja  va2 s .com*/
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, File file,
        float reqWidthInDip, float reqHeightInDip) throws IOException {

    Bitmap bitmap;//  www . jav  a2s.  c  om
    AssetFileDescriptor fileDescriptor;
    int reqWidthInPixel = (int) dipToPixels(res, reqWidthInDip);
    int reqHeightInPixel = (int) dipToPixels(res, reqHeightInDip);
    fileDescriptor = contentResolver.openAssetFileDescriptor(Uri.fromFile(file), "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:Main.java

public static Uri getOutputMediaFileUri() {
    File mediaStorageDir = new File(getCachePath());
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }//w  w  w  .j  a va  2s.c  o m
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return Uri.fromFile(mediaFile);
}

From source file:Main.java

public static void install(Activity activity, File apkFile) {
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    activity.startActivity(intent);//from  www. j a  va2  s.c  om
}

From source file:Main.java

/**
 * Checks if file passed is an image//from w ww. j a v  a  2s.c o m
 * @param file
 * @return true/false
 */
public static boolean isImage(File file) {
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString().toLowerCase());
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

    return (mimeType != null && mimeType.startsWith("image/"));
}

From source file:Main.java

public static Intent getImageFromGalleryCamera(Context context) {
    // Determine Uri of camera image to save.
    File root = new File(
            Environment.getExternalStorageDirectory() + File.separator + "dianta/camera" + File.separator);
    root.mkdirs();// w w  w .j  a va 2  s  . c  o  m
    String fname = "dianta-" + System.currentTimeMillis() + ".jpg";
    File sdImageMainDirectory = new File(root, fname);
    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // camera
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager lPackageManager = context.getPackageManager();
    List<ResolveInfo> listCam = lPackageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo rInfo : listCam) {
        String packageName = rInfo.activityInfo.packageName;
        Intent lIntent = new Intent(captureIntent);
        lIntent.setComponent(new ComponentName(rInfo.activityInfo.packageName, rInfo.activityInfo.name));
        lIntent.setPackage(packageName);
        //save camera result to external storage
        lIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(lIntent);
    }

    //ugly hacks for camera helper
    lastCameraImageSaved = outputFileUri;

    // gallery
    Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_PICK);

    Intent chooserIntent = Intent.createChooser(galleryIntent, "Pilih Sumber");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    return chooserIntent;
}

From source file:Main.java

public static void openImage(Context mContext, String imagePath) {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri = Uri.fromFile(new File(imagePath));
    intent.setDataAndType(uri, "image/*");
    mContext.startActivity(intent);// ww w  .j a  v a  2s  .  c  om
}

From source file:Main.java

public static void openVideo(Context mContext, String videoPath) {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(videoPath));
    intent.setDataAndType(uri, "video/*");
    mContext.startActivity(intent);//  w w  w  . j  ava 2 s .c o  m
}

From source file:Main.java

public static Uri createImageFile() {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image;//ww  w .j  ava 2s .  co m
    try {
        image = File.createTempFile(imageFileName, /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        Log.d(TAG, "create tmp file error!", e);
        return null;
    }
    return Uri.fromFile(image);
}