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 void installApk(Context context, File file) {

    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setType("application/vnd.android.package-archive");
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);/*from w  w  w.j a v  a 2  s .  co  m*/

}

From source file:Main.java

private static void addTomMediaScanner(Context context, File file) {
    addTomMediaScanner(context, Uri.fromFile(file));
}

From source file:Main.java

/**
 * Quickly converts path to URIs, which are mandatory in libVLC.
 *
 * @param path The path to be converted.
 * @return A URI representation of path//from   w  ww  . j  a  v a2 s .c om
 */
public static Uri PathToUri(String path) {
    return Uri.fromFile(new File(path));
}

From source file:Main.java

/**
 * /*  w w w. ja  v a  2  s  .co m*/
 * @param context
 * @param uri
 *            uri of SCHEME_FILE or SCHEME_CONTENT
 * @return image path; uri will be changed to SCHEME_FILE
 */
public static String uriToImagePath(Context context, Uri uri) {
    if (context == null || uri == null) {
        return null;
    }

    String imagePath = null;
    String uriString = uri.toString();
    String uriSchema = uri.getScheme();
    if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
        imagePath = uriString.substring("file://".length());
    } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor.getCount() == 0) {
            Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
            return null;
        }
        cursor.moveToFirst();
        imagePath = cursor.getString(1);
        // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
        uri = Uri.fromFile(new File(imagePath));
    }
    Log.v(TAG, "Final uri: " + uri.toString());
    return imagePath;
}

From source file:Main.java

/**
 * Save an image URI into the gallery// ww w .  java  2  s  . co m
 * @param context the context.
 * @param sourceFile the image path to save.
 */
public static String saveImageIntoGallery(Context context, File sourceFile) {
    String filePath = saveFileInto(context, sourceFile, Environment.DIRECTORY_PICTURES, null);

    if (null != filePath) {
        // This broadcasts that there's been a change in the media directory
        context.sendBroadcast(
                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
    }

    return filePath;
}

From source file:Main.java

public static void setClickable(final TextView textView) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    Spannable sp = (Spannable) textView.getText();
    ImageSpan[] images = sp.getSpans(0, textView.getText().length(), ImageSpan.class);

    for (ImageSpan span : images) {
        final String image_src = span.getSource();
        final int start = sp.getSpanStart(span);
        final int end = sp.getSpanEnd(span);

        ClickableSpan click_span = new ClickableSpan() {
            @Override//ww  w . j  a  va2  s. c  o  m
            public void onClick(View widget) {
                String[] strs = image_src.split("/");
                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LilyClient/"
                        + strs[strs.length - 2] + "-" + strs[strs.length - 1];

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setType("image/*");
                intent.setDataAndType(Uri.fromFile(new File(filePath)), "image/*");
                textView.getContext().startActivity(intent);

            }
        };
        ClickableSpan[] click_spans = sp.getSpans(start, end, ClickableSpan.class);
        if (click_spans.length != 0) {
            for (ClickableSpan c_span : click_spans) {
                sp.removeSpan(c_span);
            }
        }
        sp.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:Main.java

/**
 * Converts the content:// scheme to the file:// scheme
 * @param contentResolver Provides access to the content model
 * @param contentUri The URI to be converted using content:// scheme
 * @return The converted URI using file:// scheme
 *///  w w w .j a v a  2s . c  o m
public static Uri getFileUriFromContentUri(ContentResolver contentResolver, Uri contentUri) {
    String filePath = getPathFromContentUri(contentResolver, contentUri);
    if (filePath == null) {
        return null;
    }
    return Uri.fromFile(new File(filePath));
}

From source file:Main.java

private static void initData(Activity activity) {
    mActivity = activity;/* w  w  w .ja  v a  2  s .  co  m*/
    File mFile = new File(Environment.getExternalStorageDirectory() + "/yourName");
    if (!mFile.exists())
        mFile.mkdirs();
    mAvatar = new File(TEMP_IMG_PATH);
    if (mAvatar.exists())
        mAvatar.delete();
    mAvatarUri = Uri.fromFile(mAvatar);
}

From source file:Main.java

public static void openTextFile(Context context, String path) {
    File folder = Environment.getExternalStorageDirectory();
    File file = new File(folder, path);
    Uri uri = Uri.fromFile(file);

    viewUriForExtension(context, uri, "txt");
}

From source file:Main.java

public static void scanFile(String path, Context c) {
    System.out.println(path + " " + Build.VERSION.SDK_INT);
    if (Build.VERSION.SDK_INT >= 19) {
        MediaScannerConnection.scanFile(c, new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

                    @Override//w w  w. j  a  v  a  2  s . c o m
                    public void onScanCompleted(String path, Uri uri) {

                    }
                });
    } else {
        Uri contentUri = Uri.fromFile(new File(path));
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);
        c.sendBroadcast(mediaScanIntent);
    }
}