List of usage examples for android.net Uri getPath
@Nullable public abstract String getPath();
From source file:Main.java
public static File getFile(Context context, Uri uri, boolean forceCreation) { if (!forceCreation && "file".equalsIgnoreCase(uri.getScheme())) { return new File(uri.getPath()); }// w ww . ja va 2s. c om File file = null; try { File root = context.getFilesDir(); if (root == null) { throw new Exception("data dir not found"); } file = new File(root, getFilename(context, uri)); file.delete(); InputStream is = context.getContentResolver().openInputStream(uri); OutputStream os = new FileOutputStream(file); byte[] buf = new byte[1024]; int cnt = is.read(buf); while (cnt > 0) { os.write(buf, 0, cnt); cnt = is.read(buf); } os.close(); is.close(); file.deleteOnExit(); } catch (Exception e) { Log.e("OpenFile", e.getMessage(), e); } return file; }
From source file:Main.java
/** * Try to return the absolute file path from the given Uri * * @param context// w ww . ja v a 2s .c o m * @param uri * @return the file path or null */ public static String uri2FilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; }
From source file:Main.java
private static int getOrientationFromExif(Uri imageUri, Context context) { int orientation = -1; Log.i("Photo Editor", "imageUri = " + imageUri); // File imageFile = new File(getRealPathFromUri(imageUri, context)); File imageFile = new File(imageUri.getPath()); try {// w w w. j a v a2 s .c o m ExifInterface exif; Log.i("Photo Editor", "imageFile.getAbsolutePath() = " + imageFile.getAbsolutePath()); exif = new ExifInterface(imageFile.getAbsolutePath()); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); } catch (IOException e) { e.printStackTrace(); } return orientation; }
From source file:Main.java
public static String extractFilenameFromUri(Uri uri, Activity activity) { String fileName = null;/*from ww w . j a v a 2 s. com*/ if (uri == null) { throw new IllegalArgumentException(); } String scheme = uri.getScheme(); String path = uri.getPath(); if (path != null && scheme != null && scheme.equals("file")) { fileName = path.substring(path.lastIndexOf("/") + 1); } String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME /* col1 */ }; Cursor c = activity.managedQuery(uri, projection, null, null, null); if (c != null && c.moveToFirst()) { fileName = c.getString(0); } return fileName; }
From source file:Main.java
private static String getRealPathFromURI(Context context, Uri contentURI) { String result;/* w ww . j av a2s .c o m*/ Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close(); } return result; }
From source file:Main.java
public static String getRealPathFromURI(Context context, Uri contentURI) { String TAG = "PINGUINO-getRealPathFromURI"; String result;//from w w w . ja v a 2 s . c o m Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { result = contentURI.getPath(); } else { Log.d(TAG, "cursor1:" + cursor); cursor.moveToFirst(); Log.d(TAG, "cursor2:" + cursor); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); Log.d(TAG, "idx:" + idx); result = cursor.getString(idx); Log.d(TAG, "result:" + result); cursor.close(); } return result; }
From source file:Main.java
@Nullable public static File getFromMediaUri(Context context, ContentResolver resolver, Uri uri) { if (uri == null) return null; if (SCHEME_FILE.equals(uri.getScheme())) { return new File(uri.getPath()); } else if (SCHEME_CONTENT.equals(uri.getScheme())) { final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME }; Cursor cursor = null;/*from ww w . ja va 2s .c o m*/ try { cursor = resolver.query(uri, filePathColumn, null, null, null); if (cursor != null && cursor.moveToFirst()) { final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d")) ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME) : cursor.getColumnIndex(MediaStore.MediaColumns.DATA); // Picasa images on API 13+ if (columnIndex != -1) { String filePath = cursor.getString(columnIndex); if (!TextUtils.isEmpty(filePath)) { return new File(filePath); } } } } catch (IllegalArgumentException e) { // Google Drive images return getFromMediaUriPfd(context, resolver, uri); } catch (SecurityException ignored) { // Nothing we can do } finally { if (cursor != null) cursor.close(); } } return null; }
From source file:Main.java
public static String encodeQuery(String url) { Uri uri = Uri.parse(url); try {// w w w. ja v a 2 s . c o m String query = uri.getQuery(); String encodedQuery = query != null ? URLEncoder.encode(query, "UTF-8") : null; URI tmp = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment()); return tmp + (encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : ""); } catch (UnsupportedEncodingException ignore) { } catch (URISyntaxException ignore) { } return uri.toString(); }
From source file:Main.java
public static int defineExifOrientation(Uri imageUri, String mimeType) { int rotation = 0; if ("image/jpeg".equalsIgnoreCase(mimeType) && "file".equals(imageUri.getScheme())) { try {//from w ww. j av a2s . c om ExifInterface exif = new ExifInterface(imageUri.getPath()); int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); Log.e("dsd", "exifOrientation:" + exifOrientation); switch (exifOrientation) { case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: case ExifInterface.ORIENTATION_NORMAL: rotation = 0; break; case ExifInterface.ORIENTATION_TRANSVERSE: case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break; case ExifInterface.ORIENTATION_FLIP_VERTICAL: case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break; case ExifInterface.ORIENTATION_TRANSPOSE: case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break; } } catch (IOException e) { Log.e("dsd", "Can't read EXIF tags from file [%s]" + imageUri); } } return rotation; }
From source file:Main.java
/** * Get bitmap from internal image file.//ww w. j a v a2 s . c o m */ public static Bitmap getBitmapFromUri(Uri fileUri) { // bitmap factory BitmapFactory.Options options = new BitmapFactory.Options(); // downsizing photoImage as it throws OutOfMemory Exception for larger // images options.inSampleSize = 8; options.inMutable = true; return BitmapFactory.decodeFile(fileUri.getPath(), options); }