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:de.appplant.cordova.emailcomposer.EmailComposerImpl.java

/**
 * The URI for a resource.//from  ww  w  .ja v  a  2s.co m
 *
 * @param path
 * The given relative path.
 * @param ctx
 * The application context.
 * @return
 * The URI pointing to the given path
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
private Uri getUriForResourcePath(String path, Context ctx) {
    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 = ctx.getExternalCacheDir();

    if (dir == null) {
        Log.e("EmailComposer", "Missing external cache dir");
        return Uri.EMPTY;
    }

    String storage = dir.toString() + ATTACHMENT_FOLDER;
    int resId = getResId(resPath, ctx);
    File file = new File(storage, resName + extension);

    if (resId == 0) {
        Log.e("EmailComposer", "File not found: " + resPath);
    }

    new File(storage).mkdir();

    try {
        Resources res = ctx.getResources();
        FileOutputStream outStream = new FileOutputStream(file);
        InputStream inputStream = res.openRawResource(resId);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Uri.fromFile(file);
}

From source file:com.qsoft.components.gallery.utils.GalleryUtils.java

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

From source file:com.awrtechnologies.carbudgetsales.MainActivity.java

public void imageOpen(Fragment f, final int REQUEST_CAMERA, final int SELECT_FILE) {

    GeneralHelper.getInstance(MainActivity.this).setTempFragment(f);
    final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override/*  w ww  . j  a v  a 2  s .  c  o m*/
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {

                java.io.File imageFile = new File(
                        (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/."
                                + Constants.APPNAME + "/" + System.currentTimeMillis() + ".jpeg"));

                PreferencesManager.setPreferenceByKey(MainActivity.this, "IMAGEWWC",
                        imageFile.getAbsolutePath());
                //
                imageFilePath = imageFile;
                imageFileUri = Uri.fromFile(imageFile);
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
                startActivityForResult(i, REQUEST_CAMERA);

            } else if (items[item].equals("Choose from Library")) {
                if (Build.VERSION.SDK_INT < 19) {
                    Intent intent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
                } else {
                    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("image/*");
                    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
                }

            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

From source file:org.mythdroid.util.UpdateService.java

private void updateMythDroid(String ver, String url) {

    LogUtil.debug("Fetching APK from " + url); //$NON-NLS-1$

    HttpFetcher fetcher = null;//from  w ww .  j  a v a 2s .co  m
    try {
        fetcher = new HttpFetcher(url, false);
    } catch (ClientProtocolException e) {
        ErrUtil.logErr(e);
        notify("MythDroid" + Messages.getString("UpdateService.2"), //$NON-NLS-1$ //$NON-NLS-2$
                e.getMessage());
        return;
    } catch (IOException e) {
        ErrUtil.logErr(e);
        notify("MythDroid" + Messages.getString("UpdateService.2"), //$NON-NLS-1$ //$NON-NLS-2$
                e.getMessage());
        return;
    }

    final File storage = Environment.getExternalStorageDirectory();
    final File outputFile = new File(storage.getAbsolutePath() + '/' + "Download", //$NON-NLS-1$
            "MythDroid-" + ver + ".apk" //$NON-NLS-1$ //$NON-NLS-2$
    );

    LogUtil.debug("Saving to " + outputFile.getAbsolutePath()); //$NON-NLS-1$

    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        ErrUtil.logErr("SDCard is unavailable"); //$NON-NLS-1$
        notify("MythDroid" + Messages.getString("UpdateService.2"), //$NON-NLS-1$ //$NON-NLS-2$
                Messages.getString("UpdateService.4") //$NON-NLS-1$
        );
        return;
    }

    try {
        fetcher.writeTo(outputStream);
    } catch (IOException e) {
        ErrUtil.logErr(e);
        notify("MythDroid" + Messages.getString("UpdateService.2"), //$NON-NLS-1$ //$NON-NLS-2$
                e.getMessage());
        return;
    }

    LogUtil.debug("Download successful, installing..."); //$NON-NLS-1$

    final Intent installIntent = new Intent(Intent.ACTION_VIEW);
    installIntent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive" //$NON-NLS-1$
    );
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(installIntent);

}

From source file:com.pspdfkit.cordova.PSPDFCordovaPlugin.java

/**
 * Starts the {@link PSPDFActivity} to show a single document stored within the app's assets.
 *
 * @param assetPath       Relative path inside the app's assets folder.
 * @param configuration   PSPDFKit configuration.
 * @param callbackContext Cordova callback.
 *//*from  w ww  .  j a  va  2  s  .  c  o m*/
private void showDocumentFromAssets(@NonNull final String assetPath,
        @NonNull final PSPDFActivityConfiguration configuration,
        @NonNull final CallbackContext callbackContext) {
    ExtractAssetTask.extract(assetPath, cordova.getActivity(),
            new ExtractAssetTask.OnDocumentExtractedCallback() {
                @Override
                public void onDocumentExtracted(File documentFile) {
                    if (documentFile != null) {
                        showDocumentForUri(Uri.fromFile(documentFile), configuration);
                        callbackContext.success();
                    } else {
                        callbackContext.error("Could not load '" + assetPath + "' from the assets.");
                    }
                }
            });
}

From source file:com.grupohqh.carservices.operator.ManipulateCarActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CAPTURE_IMAGE:
            final File file = getTempFile(this);
            try {
                bm = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
                imgCar.setImageBitmap(bm);
                // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
            } catch (FileNotFoundException e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }//from www . j a v a2  s  . c om
            break;
        }
    }
}

From source file:com.insthub.O2OMobile.Activity.F9_SettingActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File files = new File(mFileName);
            if (files.exists()) {
                mImagePath = mFileName;// w  ww . ja v  a2 s.  c om
                mImagePath = startPhotoZoom(Uri.fromFile(new File(mImagePath)));
            }
        } else if (requestCode == REQUEST_PHOTO) {
            Uri selectedImage = data.getData();
            mImagePath = startPhotoZoom(selectedImage);
        } else if (requestCode == REQUEST_PHOTOZOOM) {
            File f = new File(mImagePath);
            if (f.exists()) {
                File file = new File(ImageUtil.zoomImage(mImagePath, 350));
                mUserBalance.changeAvatar(file);
            } else {
                ToastView toast = new ToastView(this, getString(R.string.photo_not_exsit));
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    }
}

From source file:com.firesoft.member.Activity.F9_SettingActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File files = new File(mFileName);
            if (files.exists()) {
                mImagePath = mFileName;//from   ww  w  .  java2  s. c om
                mImagePath = startPhotoZoom(Uri.fromFile(new File(mImagePath)));
            }
        } else if (requestCode == REQUEST_PHOTO) {
            Uri selectedImage = data.getData();
            mImagePath = startPhotoZoom(selectedImage);
        }
    }
}

From source file:es.upv.riromu.arbre.main.MainActivity.java

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("cropping")) {
        state[CROP_IMAGE] = savedInstanceState.getBoolean("cropping");
    }/*from  w  ww. j  av a  2 s.c  o  m*/
    if (savedInstanceState.containsKey("treated")) {
        state[TREAT_IMAGE] = savedInstanceState.getBoolean("treated");
    }

    if (savedInstanceState.containsKey("image_uri")) {
        image_uri = Uri.parse(savedInstanceState.getString("image_uri"));
    }

    if (savedInstanceState.getBoolean("image")) {
        String fileName = "temp_image.jpg";
        String fileURL = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()
                + "/" + fileName;
        File file = new File(fileURL);
        try {
            InputStream imageStream = getContentResolver().openInputStream(Uri.fromFile(file));
            image = Util.decodeScaledBitmapFromFile(file.getPath(), MAX_SIZE, MAX_SIZE);
            //  image = BitmapFactory.decodeStream(imageStream);

        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }
    }

    if (state[CROP_IMAGE]) {
        String fileName = "temp_cropped.jpg";
        String fileURL = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()
                + "/" + fileName;
        File file = new File(fileURL);
        try {
            InputStream imageStream = getContentResolver().openInputStream(Uri.fromFile(file));
            croppedimage = BitmapFactory.decodeStream(imageStream);
            imageStream.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }
    if (state[TREAT_IMAGE]) {
        String fileName = "temp_treated.jpg";
        String fileURL = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()
                + "/" + fileName;
        File file = new File(fileURL);
        try {
            InputStream imageStream = getContentResolver().openInputStream(Uri.fromFile(file));
            ImageView imv = (ImageView) findViewById(R.id.image_intro);
            imv.setImageBitmap(BitmapFactory.decodeStream(imageStream));
            imageStream.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }
    ImageView imv = (ImageView) findViewById(R.id.image_intro);
    colours = savedInstanceState.getIntArray("colours");
    if ((!state[TREAT_IMAGE]) && state[CROP_IMAGE])
        imv.setImageBitmap(croppedimage);
    else {

        imv.setImageBitmap(image);
    }
    TextView imc = (TextView) findViewById(R.id.textView);
    imc.setBackgroundColor(Color.rgb(colours[COLOUR_R], colours[COLOUR_G], colours[COLOUR_B]));
}

From source file:com.polyvi.xface.extension.XAppExt.java

/**
 * ?pathuri/*from   w  ww . j a v  a  2  s.c  o  m*/
 *
 * @param app
 *            
 * @param path
 *            url return Uri ?:uri,:null
 */
private Uri getUrlFromPath(XIWebContext webContext, String path) {
    XPathResolver fileResolver = new XPathResolver(path, webContext.getWorkSpace());
    String absPath = fileResolver.resolve();
    Uri uri = null;
    if (path.startsWith(XConstant.HTTP_SCHEME) || path.startsWith(XConstant.HTTPS_SCHEME)) {
        uri = Uri.parse(absPath);
    } else {
        File file = new File(absPath);
        if (file.exists()) {
            uri = Uri.fromFile(file);
        }
    }
    return uri;
}