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:com.poinsart.votar.VotarMain.java

private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

From source file:de.appplant.cordova.plugin.notification.Asset.java

/**
 * The URI for a resource./* w w w .j a va 2s  . co  m*/
 * 
 * @param path
 *            The given relative path
 * 
 * @return The URI pointing to the given path
 */
private Uri getUriForResourcePath(String path) {
    String resPath = path.replaceFirst("res://", "");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String resName = fileName.substring(0, fileName.lastIndexOf('.'));
    String extension = resPath.substring(resPath.lastIndexOf('.'));
    File dir = activity.getExternalCacheDir();
    if (dir == null) {
        Log.e("Asset", "Missing external cache dir");
        return Uri.EMPTY;
    }
    String storage = dir.toString() + STORAGE_FOLDER;
    int resId = getResId(resPath);
    File file = new File(storage, resName + extension);
    if (resId == 0) {
        Log.e("Asset", "File not found: " + resPath);
        return Uri.EMPTY;
    }
    new File(storage).mkdir();
    try {
        Resources res = activity.getResources();
        FileOutputStream outStream = new FileOutputStream(file);
        InputStream inputStream = res.openRawResource(resId);
        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
        return Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Uri.EMPTY;
}

From source file:de.stadtrallye.rallyesoft.model.pictures.PictureManager.java

private Uri getPicturePlaceholderUri(int mediaType, SourceHint sourceHint) {
    Uri uri = Uri.fromFile(getPicturePlaceholder(mediaType));

    saveReservedFilename(uri, sourceHint);
    return uri;//from ww w  .  ja v  a 2s  .  c om
}

From source file:com.app.swaedes.swaedes.ConstructionSitePage.java

public Uri getOutputAudioFileUri(int type) {
    return Uri.fromFile(getOutputAudioFile(type));
}

From source file:foam.zizim.android.BoskoiService.java

private void showNotification(String tickerText) {
    // This is what should be launched if the user selects our notification.
    Intent baseIntent = new Intent(this, IncidentsTab.class);
    baseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, baseIntent, 0);

    // choose the ticker text
    newBoskoiReportNotification = new Notification(R.drawable.favicon, tickerText, System.currentTimeMillis());
    newBoskoiReportNotification.contentIntent = contentIntent;
    newBoskoiReportNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newBoskoiReportNotification.defaults = Notification.DEFAULT_ALL;
    newBoskoiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent);
    if (ringtone) {
        // set the ringer
        Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3"));
        newBoskoiReportNotification.sound = ringURI;
    }/* ww w.j  a va2s  . c om*/

    if (vibrate) {
        double vibrateLength = 100 * Math.exp(0.53 * 20);
        long[] vibrate = new long[] { 100, 100, (long) vibrateLength };
        newBoskoiReportNotification.vibrate = vibrate;

        if (flashLed) {
            int color = Color.BLUE;
            newBoskoiReportNotification.ledARGB = color;
        }

        newBoskoiReportNotification.ledOffMS = (int) vibrateLength;
        newBoskoiReportNotification.ledOnMS = (int) vibrateLength;
        newBoskoiReportNotification.flags = newBoskoiReportNotification.flags | Notification.FLAG_SHOW_LIGHTS;
    }

    mNotificationManager.notify(NOTIFICATION_ID, newBoskoiReportNotification);
}

From source file:com.xengar.android.booksearch.ui.BookDetailActivity.java

public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;/*from w w w  .j  ava2  s . c  om*/
    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(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                "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:com.cloudexplorers.plugins.childBrowser.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 * //from  w w  w .j a  va2 s .c o m
 * @param url
 *          The url to load.
 * @param usePhoneGap
 *          Load url in PhoneGap webview
 * @return "" if ok, or error message.
 */
public String openExternal(String urlToOpen, String encodedCredentials) {
    try {
        // set the download URL, a url that points to a file on the internet
        // this is the file to be downloaded
        URL url = new URL(urlToOpen);

        // create the new connection
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        // set up some things on the connection
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        urlConnection.setRequestProperty("Authorization", encodedCredentials);

        // and connect!
        urlConnection.connect();

        // set the path where we want to save the file
        // in this case, going to save it on the root directory of the
        // sd card.
        File SDCardRoot = Environment.getExternalStorageDirectory();
        // create a new file, specifying the path, and the filename
        // which we want to save the file as.
        File file = new File(SDCardRoot, "temp.pdf");

        // this will be used to write the downloaded data into the file we created
        FileOutputStream fileOutput = new FileOutputStream(file);

        // this will be used in reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        // this is the total size of the file
        int totalSize = urlConnection.getContentLength();
        // variable to store total downloaded bytes
        int downloadedSize = 0;

        // create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0; // used to store a temporary size of the buffer

        // now, read through the input buffer and write the contents to the file
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            // add the data in the buffer to the file in the file output stream (the
            // file on the sd card
            fileOutput.write(buffer, 0, bufferLength);
            // add up the size so we know how much is downloaded
            downloadedSize += bufferLength;

        }
        // close the output stream when done
        fileOutput.close();

        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.cordova.getActivity().startActivity(intent);

        // catch some possible errors...
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";

}

From source file:com.phonegap.Capture.java

/**
 * Sets up an intent to capture images.  Result handled by onActivityResult()
 *//*from w  ww. j  a v a 2s  .co  m*/
private void captureImage() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    // Specify file so that large image is captured and returned
    File photo = new File(DirectoryManager.getTempDirectoryPath(ctx), "Capture.jpg");
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    this.imageUri = Uri.fromFile(photo);

    this.ctx.startActivityForResult((Plugin) this, intent, CAPTURE_IMAGE);
}

From source file:org.mobisocial.corral.ContentCorral.java

public static Uri storeContent(Context context, byte[] raw, String type) {
    File contentDir;//from ww w  . j  a  v  a 2 s  . co  m
    if (type != null && (type.startsWith("image/") || type.startsWith("video/"))) {
        contentDir = new File(Environment.getExternalStorageDirectory(), PICTURE_SUBFOLDER);
    } else {
        contentDir = new File(Environment.getExternalStorageDirectory(), FILES_SUBFOLDER);
    }

    if (!contentDir.exists() && !contentDir.mkdirs()) {
        Log.e(TAG, "failed to create musubi corral directory");
        return null;
    }
    int timestamp = (int) (System.currentTimeMillis() / 1000L);
    String ext = CorralDownloadClient.extensionForType(type);
    String fname = timestamp + "-" + "webapp-raw" + "." + ext;
    File copy = new File(contentDir, fname);
    FileOutputStream out = null;
    try {
        contentDir.mkdirs();
        out = new FileOutputStream(copy);
        out.write(raw);
        return Uri.fromFile(copy);
    } catch (IOException e) {
        Log.w(TAG, "Error copying file", e);
        if (copy.exists()) {
            copy.delete();
        }
        return null;
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
            Log.e(TAG, "failed to close handle on store corral content", e);
        }
    }
}

From source file:org.zywx.wbpalmstar.engine.EDownloadDialog.java

private void downloadDone() {
    stopDownload();/*w ww.jav a  2  s .c  o  m*/
    Intent installIntent = new Intent(Intent.ACTION_VIEW);
    String filename = mTmpFile.getAbsolutePath();
    Uri path = Uri.parse(filename);
    if (path.getScheme() == null) {
        path = Uri.fromFile(new File(filename));
    }
    installIntent.setDataAndType(path, mimetype);
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        getContext().startActivity(installIntent);
    } catch (Exception e) {
        e.printStackTrace();
        mProgressHandler.sendMessage(mProgressHandler.obtainMessage(-2, "?"));
    }
}