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

/**
 * Creates a file Uri for a file defined by its absolute path.
 * The method can handle the case of an absolute path (e.g. /data/data....)
 * and a Uri path containing the file:// scheme (e.g. file:///data/data...)
 *///from   w w  w.j  a  v  a  2s  . co  m
public static Uri createFileUri(String path) {
    if (path.startsWith("file://")) {
        return Uri.parse(path);
    }
    return Uri.fromFile(new File(path));
}

From source file:Main.java

public static void installApk(Context context, File apkfile) {
    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setType("application/vnd.android.package-archive");
    intent.setData(Uri.fromFile(apkfile));
    intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);//from w w w  .  j a  va2  s .com
}

From source file:Main.java

public static boolean insertMediaStore(Context context, File file, String imageName) {
    if (context == null || file == null || !file.exists()) {
        return false;
    }// w w  w .  j a  va 2s  .c om

    if (TextUtils.isEmpty(imageName)) {
        imageName = SystemClock.currentThreadTimeMillis() + PNG;
    }
    try {

        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), imageName,
                null);
        // String imagePath =
        // MediaStore.Images.Media.insertImage(getContentResolver(),
        // bitmap, "", "");

        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }

    return true;

}

From source file:Main.java

public static Uri getOutputImageFileURL() {
    Log.d(LOG_MESSAGE, "external dir:"
            + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(LOG_MESSAGE, "failed to create directory");
            return null;
        }/* w  w w  .jav a  2s.  co m*/
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + "IMG_" + timeStamp + ".png");
    return Uri.fromFile(mediaFile);
}

From source file:Main.java

/**
 * Helper method for adding the photo to the system photo gallery so it can be accessed
 * from other apps./*  w w w  .j a v  a  2  s  . co m*/
 *
 * @param imagePath The path of the saved image
 */
private static void galleryAddPic(Context context, String imagePath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(imagePath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
}

From source file:Main.java

public static Uri FileToUri(File file) {
    return Uri.fromFile(file);
}

From source file:Main.java

/**
 * Creates a intent with an image//from   w  ww .  j  a  v  a  2s.co m
 *
 * @param image
 * @return
 */
public static Intent createIntentFromImage(File image) {
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    return share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
}

From source file:Main.java

public static Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;/*  ww w .j av a2 s.  co  m*/
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

From source file:Main.java

private static void scanPhoto(Context ctx, String imgFileName) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File file = new File(imgFileName);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    ctx.sendBroadcast(mediaScanIntent);/*ww w. j  a  v  a  2  s. c  om*/
}

From source file:Main.java

public static void cropImgFromSelfCenter(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 110);
    intent.putExtra("aspectY", 135);
    intent.putExtra("outputX", 110);
    intent.putExtra("outputY", 135);
    intent.putExtra("return-data", false);
    intent.putExtra("noFaceDetection", true);
    File mFile = new File(Environment.getExternalStorageDirectory() + "/yourName");
    if (!mFile.exists())
        mFile.mkdirs();//from  w  w w . ja  v a2 s  .  com
    mCropAvatar = new File(CROP_IMG_PATH);
    if (mCropAvatar.exists())
        mCropAvatar.delete();
    mCropUri = Uri.fromFile(mCropAvatar);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCropUri);
    mActivity.startActivityForResult(intent, REQUEST_CROP_RETURN_SELF_CENTER);
}