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.snipme.record.Record.java

/**
 * Called when the video view exits./*from w  w w .  j  a v  a2  s .  c  om*/
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 * @throws JSONException
 */
public void onActivityResult(int requestCode, int resultCode, final Intent intent) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAPTURE_VIDEO) {
            Log.i(TAG, "onActivityResult: Video Capture success");
            final Record that = this;
            Runnable captureVideo = new Runnable() {

                @Override
                public void run() {

                    Uri data = null;

                    if (intent != null) {
                        // Get the uri of the video clip
                        data = intent.getData();
                        Log.i(TAG, "onActivityResult: Uri: " + data);
                        Log.i(TAG, "Uri of the video file recorded");
                    }

                    if (data == null) {
                        File movie = new File(getTempDirectoryPath(), "Capture.avi");
                        data = Uri.fromFile(movie);
                    }

                    // create a file object from the uri
                    if (data == null) {
                        that.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null"));
                    } else {
                        results.put(createMediaFile(data));

                        if (results.length() >= 1) {
                            // Send Uri back to JavaScript for viewing video
                            that.callbackContext
                                    .sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
                        } else {
                            // still need to capture more video clips
                            //captureVideo(duration);
                        }
                    }
                }
            };
            this.cordova.getThreadPool().execute(captureVideo);
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {

    }

}

From source file:foam.jellyfish.StarwispBuilder.java

public static void email(Context context, String emailTo, String emailCC, String subject, String emailText,
        List<String> filePaths) {
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { emailTo });
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[] { emailCC });
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

    ArrayList<String> extra_text = new ArrayList<String>();
    extra_text.add(emailText);/*w  ww. j  a  v  a 2  s .co  m*/
    emailIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text);
    //emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);

    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths) {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

From source file:com.xixicm.de.data.storage.SentenceDataRepository.java

@Override
public Uri getAudioFile(String audioUrl) {
    DiskBasedCache cache = DEVolley.getInstance().getCache();
    if (audioUrl != null && cache != null) {
        File audioFile = cache.getFileForKey(audioUrl);
        if (audioFile != null && audioFile.exists()) {
            return Uri.fromFile(audioFile);
        }/*  ww w . j  ava  2s  .  c  o  m*/
    }
    return null;
}

From source file:com.google.cloud.solutions.smashpix.MainActivity.java

  /**
 * Takes a picture and stores the image locally.
 */// w  ww . j a  v a 2s .  co m
public void takePicture()  {
  String timeStamp = new SimpleDateFormat(DATETIME_FORMAT).format(new Date());
  String imageFileName = CAMERA_FILENAME_PREFIX + timeStamp + JPEG_EXTENSION;
  imageStoragePath = new File(imageStorageDirectory, imageFileName);
  Uri imageStorageUri = Uri.fromFile(imageStoragePath);

  Intent intent = new Intent(IMAGE_CAPTURE_INTENT);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageStorageUri);
  startActivityForResult(intent, Constants.TAKE_PICTURE);
}

From source file:com.polyvi.xface.extension.camera.XCameraExt.java

public void takePicture(int mEncodingType) {
    Cursor cursor = queryImgDB();
    if (null != cursor) {
        mNumPics = cursor.getCount();// ??
    } else {//w w  w .j a v a 2s .c  om
        mNumPics = 0;//Cursor
    }

    // ?camera?
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = createCaptureFile(mEncodingType); // 

    // ?URI???
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    mImageUri = Uri.fromFile(photo);

    // ?Camera??onActivityResult?
    mExtensionContext.getSystemContext().startActivityForResult(this, intent, CAMERA_REQUEST_CODE);
}

From source file:com.zia.freshdocs.widget.CMISAdapter.java

/**
 * Send the content using a built-in Android activity which can handle the content type.
 * @param position//from   w w  w. j a v  a 2s  . c om
 */
public void shareContent(int position) {
    final NodeRef ref = getItem(position);
    downloadContent(ref, new Handler() {
        public void handleMessage(Message msg) {
            Context context = getContext();
            boolean done = msg.getData().getBoolean("done");

            if (done) {
                dismissProgressDlg();

                File file = (File) _dlThread.getResult();

                if (file != null) {
                    Resources res = context.getResources();
                    Uri uri = Uri.fromFile(file);
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, ref.getName());
                    emailIntent.putExtra(Intent.EXTRA_TEXT, res.getString(R.string.email_text));
                    emailIntent.setType(ref.getContentType());

                    try {
                        context.startActivity(
                                Intent.createChooser(emailIntent, res.getString(R.string.email_title)));
                    } catch (ActivityNotFoundException e) {
                        String text = "No suitable applications registered to send " + ref.getContentType();
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }
                }
            } else {
                int value = msg.getData().getInt("progress");
                if (value > 0) {
                    _progressDlg.setProgress(value);
                }
            }
        }
    });
}

From source file:de.appplant.cordova.plugin.emailcomposer.EmailComposer.java

/**
 * The URI for a file./*from w w  w.j  a v  a 2s .  c  om*/
 *
 * @param {String} path
 *      The given absolute path
 *
 * @return The URI pointing to the given path
 */
private Uri getUriForAbsolutePath(String path) {
    String absPath = path.replaceFirst("file://", "");
    File file = new File(absPath);

    if (!file.exists()) {
        System.err.println("Attachment path not found: " + file.getAbsolutePath());
    }

    return Uri.fromFile(file);
}

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

/**
 *Get Uri for HTTP Content//from   w w  w  . j  a va2s  .c o m
 * @param path HTTP adress
 * @return Uri of the downloaded file
 */
private Uri getUriForHTTP(String path) {
    try {
        URL url = new URL(path);
        String fileName = path.substring(path.lastIndexOf('/') + 1);
        String resName = fileName.substring(0, fileName.lastIndexOf('.'));
        String extension = path.substring(path.lastIndexOf('.'));
        File dir = activity.getExternalCacheDir();
        if (dir == null) {
            Log.e("Asset", "Missing external cache dir");
            return Uri.EMPTY;
        }
        String storage = dir.toString() + STORAGE_FOLDER;
        File file = new File(storage, resName + extension);
        new File(storage).mkdir();

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        FileOutputStream outStream = new FileOutputStream(file);
        copyFile(input, outStream);
        outStream.flush();
        outStream.close();

        return Uri.fromFile(file);
    } catch (MalformedURLException e) {
        Log.e("Asset", "Incorrect URL");
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        Log.e("Asset", "Failed to create new File from HTTP Content");
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("Asset", "No Input can be created from http Stream");
        e.printStackTrace();
    }
    return Uri.EMPTY;
}

From source file:com.zsxj.pda.ui.client.LoginActivity.java

private void download(String url) {
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(url);/*from   w ww .j av a 2s . c  o  m*/
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    String fileName = url.substring(url.lastIndexOf("/") + 1);
    File downloadDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    Log.d("debug", downloadDir.toString());
    File updateFile = new File(downloadDir, fileName);
    Log.d("debug", updateFile.toString());
    if (updateFile.exists()) {
        Intent promtInstall = new Intent(Intent.ACTION_VIEW);
        promtInstall.setDataAndType(Uri.fromFile(updateFile), "application/vnd.android.package-archive");
        startActivity(promtInstall);
        finish();
    } else {
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
        long downloadId = dm.enqueue(request);
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putLong(PrefKeys.DOWNLOAD_ID, downloadId);
        editor.commit();
    }
}