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

private static Uri getUriFromAsset(Context mContext, String path) {
    File dir = mContext.getExternalCacheDir();

    if (dir == null) {
        Log.e(TAG, "Missing external cache dir");
        return Uri.EMPTY;
    }/* w  w  w.  j  a v  a 2 s .c om*/
    String resPath = path.replaceFirst("file:/", "www");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String storage = dir.toString() + STORAGE_FOLDER;

    if (fileName == null || fileName.isEmpty()) {
        Log.e(TAG, "Filename is missing");
        return Uri.EMPTY;
    }

    File file = new File(storage, fileName);
    FileOutputStream outStream = null;
    InputStream inputStream = null;

    try {
        File fileStorage = new File(storage);
        if (!fileStorage.mkdir())
            Log.e(TAG, "Storage directory could not be created: " + storage);

        AssetManager assets = mContext.getAssets();
        outStream = new FileOutputStream(file);
        inputStream = assets.open(resPath);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
        return Uri.fromFile(file);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: assets/" + resPath);
    } catch (IOException ioe) {
        Log.e(TAG, "IOException occured");
    } catch (SecurityException secEx) {
        Log.e(TAG, "SecurityException: directory creation denied");
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException ioe) {
            Log.e(TAG, "IOException occured while closing/flushing streams");
        }
    }
    return Uri.EMPTY;
}

From source file:Main.java

private static Uri getUriFromPath(String path) {
    String absPath = path.replaceFirst("file://", "");
    File file = new File(absPath);

    if (!file.exists()) {
        Log.e(TAG, "File not found: " + file.getAbsolutePath());
        return Uri.EMPTY;
    }/*from   w  ww . ja  v a 2s.  co  m*/

    return Uri.fromFile(file);
}

From source file:com.github.fi3te.iliasdownloader.controller.Util.java

public static void openFile(File file, Activity forMessages) {
    if (file != null && forMessages != null && file.isFile()) {
        String extension = FilenameUtils.getExtension(file.getPath());
        if (extension.length() > 0) {
            try {
                extension = extension.toLowerCase();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                        MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension));
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                forMessages.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {
                Toast.makeText(forMessages, forMessages.getResources().getString(R.string.unknown_type),
                        Toast.LENGTH_SHORT).show();
            }//w ww. j a v  a 2 s .c  om
        }
    }
}

From source file:com.George.iexpense.activity.Echo.java

private PluginResult openFile(String fileUrl) {
    Log.d("FileViewerPlugin", "View file" + fileUrl);
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(fileUrl);
    String extension = fileUrl.substring(fileUrl.lastIndexOf(".") + 1);
    String type = "";
    if (extension.toLowerCase().equals("pdf")) {
        type = "application/pdf";
    } else if (extension.toLowerCase().equals("flv")) {
        type = "video/flv";
    } else if (extension.toLowerCase().equals("mp4")) {
        type = "video/mp4";
    } else if (extension.toLowerCase().equals("jpg")) {
        type = "image/jpg";
    }/*  w  ww  . j  a va 2 s.  c o  m*/
    intent.setDataAndType(Uri.fromFile(file), type);
    cordova.getActivity().startActivity(intent);
    Log.d("FileViewerPlugin", "View complete in" + fileUrl);
    return new PluginResult(PluginResult.Status.OK, fileUrl);
}

From source file:Main.java

/**
 * replace existing spannable with smiles
 * @param context/* w  w  w  .j a  v  a 2  s . c  om*/
 * @param spannable
 * @return
 */
public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Object> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                Object value = entry.getValue();
                if (value instanceof String && !((String) value).startsWith("http")) {
                    File file = new File((String) value);
                    if (!file.exists() || file.isDirectory()) {
                        return false;
                    }
                    spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)), matcher.start(),
                            matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannable.setSpan(new ImageSpan(context, (Integer) value), matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
    }

    return hasChanges;
}

From source file:at.wada811.utils.IntentUtils.java

/**
 * Viewer???Intent??//ww  w.j  av a  2s .  com
 *
 * @param filePath
 * @param mimeType
 * @return intent
 */
public static Intent createFileViewIntent(String filePath) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(filePath)), MediaUtils.getMimeType(filePath));
    return intent;
}

From source file:com.diona.videoplugin.CameraUtil.java

/**
 * Method to get the photo from the camera.
 * //from w  w w. j av  a  2 s  .  c om
 * @param fragment
 *          fragment instance that holds the callback method for the camera utility
 */
public static void getPhotoFromCamera(final Fragment fragment) {
    final File imageFile = FileCacheUtil.getOutputMediaFile();
    if (imageFile != null && imageFile.exists()) {
        FileUtils.deleteQuietly(imageFile);
    }
    callingActivity = fragment;
    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

    // start the image capture Intent
    callingActivity.startActivityForResult(intent, CAMERA_REQUEST);
}

From source file:com.lewie9021.videothumbnail.VideoThumbnail.java

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {/*from www . j  a v  a 2s . c om*/
        // Action is 'create'
        if (action.equals("create")) {
            Uri fileURI = Uri.fromFile(new File(args.getString(0)));
            String filePath = args.getString(0).replace(fileURI.getScheme() + ":", "");
            Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);

            callbackContext.success(encodeTobase64(thumbnail));
            return true;
        } else {
            callbackContext.error("Invalid action");
            return false;
        }
    } catch (JSONException e) {
        callbackContext.error("JSON Exception");
        return true;
    }
}

From source file:com.remobile.file.LocalFilesystem.java

public LocalFilesystem(String name, Context context, CordovaResourceApi resourceApi, File fsRoot) {
    super(Uri.fromFile(fsRoot).buildUpon().appendEncodedPath("").build(), name, resourceApi);
    this.context = context;
}

From source file:com.target.plugins.PdfViewer.java

public String showPdf(String fileName) {

    File file = new File(fileName);

    if (file.exists()) {
        try {//from   w  w w  . jav  a2 s.  c  om
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            //intent.setData(Uri.parse(fileName));
            this.ctx.startActivity(intent);
            return "";
        } catch (android.content.ActivityNotFoundException e) {
            System.out.println("PdfViewer: Error loading url " + fileName + ":" + e.toString());
            return e.toString();
        }

    } else {
        return "file not found";
    }

}