List of usage examples for android.content ContentResolver SCHEME_FILE
String SCHEME_FILE
To view the source code for android.content ContentResolver SCHEME_FILE.
Click Source Link
From source file:Main.java
/** * Try to get the exif orientation of the passed image uri * // www .j a v a2 s . co m * @param context * @param uri * @return */ public static int getExifOrientation(Context context, Uri uri) { final String scheme = uri.getScheme(); ContentProviderClient provider = null; if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) { return getExifOrientation(uri.getPath()); } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) { try { provider = context.getContentResolver().acquireContentProviderClient(uri); } catch (SecurityException e) { return 0; } if (provider != null) { Cursor result; try { result = provider.query(uri, new String[] { Images.ImageColumns.ORIENTATION, Images.ImageColumns.DATA }, null, null, null); } catch (Exception e) { e.printStackTrace(); return 0; } if (result == null) { return 0; } int orientationColumnIndex = result.getColumnIndex(Images.ImageColumns.ORIENTATION); int dataColumnIndex = result.getColumnIndex(Images.ImageColumns.DATA); try { if (result.getCount() > 0) { result.moveToFirst(); int rotation = 0; if (orientationColumnIndex > -1) { rotation = result.getInt(orientationColumnIndex); } if (dataColumnIndex > -1) { String path = result.getString(dataColumnIndex); rotation |= getExifOrientation(path); } return rotation; } } finally { result.close(); } } } return 0; }
From source file:Main.java
/** * Try to get the exif orientation of the passed image uri * * @param context// w w w. j a v a 2s. c o m * @param uri * @return */ public static int getExifOrientation(Context context, Uri uri) { final String scheme = uri.getScheme(); ContentProviderClient provider = null; if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) { return getExifOrientation(uri.getPath()); } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) { try { provider = context.getContentResolver().acquireContentProviderClient(uri); } catch (SecurityException e) { return 0; } if (provider != null) { Cursor result; try { result = provider.query(uri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION, MediaStore.Images.ImageColumns.DATA }, null, null, null); } catch (Exception e) { e.printStackTrace(); return 0; } if (result == null) { return 0; } int orientationColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION); int dataColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.DATA); try { if (result.getCount() > 0) { result.moveToFirst(); int rotation = 0; if (orientationColumnIndex > -1) { rotation = result.getInt(orientationColumnIndex); } if (dataColumnIndex > -1) { String path = result.getString(dataColumnIndex); rotation |= getExifOrientation(path); } return rotation; } } finally { result.close(); } } } return 0; }
From source file:Main.java
static String convertUriToPath(Context context, Uri uri) { Log.v(TAG, "convertUriToPath : " + uri + " @" + context); String path = null;/* www.java2 s. c om*/ if (null != uri) { String scheme = uri.getScheme(); if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) { path = uri.getPath(); } else if (scheme.equals("http")) { path = uri.toString(); } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) { String[] projection = new String[] { MediaStore.MediaColumns.DATA }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) { throw new IllegalArgumentException("Given Uri could not be found in media store"); } int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); path = cursor.getString(pathIndex); } catch (SQLiteException e) { throw new IllegalArgumentException( "Given Uri is not formatted in a way so that it can be found in media store."); } finally { if (null != cursor) { cursor.close(); } } } else { throw new IllegalArgumentException("Given Uri scheme is not supported"); } } Log.v(TAG, "convertUriToPath : >" + path); return path; }
From source file:de.kodejak.hashr.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager() .findFragmentById(R.id.navigation_drawer); mTitle = getTitle();/*from w w w.j av a 2s. c o m*/ // Set a toolbar which will replace the action bar. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Set up the drawer. mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar); Intent intent = getIntent(); String action = intent.getAction(); if (action.compareTo(Intent.ACTION_VIEW) == 0) { String scheme = intent.getScheme(); ContentResolver resolver = getContentResolver(); if (scheme.compareTo(ContentResolver.SCHEME_CONTENT) == 0) { Uri uri = intent.getData(); String name = getContentName(resolver, uri); Log.v("tag", "Content intent detected: " + action + " : " + intent.getDataString() + " : " + intent.getType() + " : " + name); //What TODO? } else if (scheme.compareTo(ContentResolver.SCHEME_FILE) == 0) { lastSumFile = intent.getData().getPath(); forcedFragmentNum = prepareOpenSumFile(lastSumFile); if (forcedFragmentNum > -1) { mNavigationDrawerFragment.selectItem(forcedFragmentNum); } } } }
From source file:com.android.gallery3d.data.UriImage.java
private int openOrDownloadInner(JobContext jc) { String scheme = mUri.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) { try {/*from w w w.j a va 2s . com*/ if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { InputStream is = mApplication.getContentResolver().openInputStream(mUri); mRotation = Exif.getOrientation(is); Utils.closeSilently(is); } mFileDescriptor = mApplication.getContentResolver().openFileDescriptor(mUri, "r"); if (jc.isCancelled()) return STATE_INIT; return STATE_DOWNLOADED; } catch (FileNotFoundException e) { Log.w(TAG, "fail to open: " + mUri, e); return STATE_ERROR; } } else { try { URL url = new URI(mUri.toString()).toURL(); mCacheEntry = mApplication.getDownloadCache().download(jc, url); if (jc.isCancelled()) return STATE_INIT; if (mCacheEntry == null) { Log.w(TAG, "download failed " + url); return STATE_ERROR; } if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { InputStream is = new FileInputStream(mCacheEntry.cacheFile); mRotation = Exif.getOrientation(is); Utils.closeSilently(is); } mFileDescriptor = ParcelFileDescriptor.open(mCacheEntry.cacheFile, ParcelFileDescriptor.MODE_READ_ONLY); return STATE_DOWNLOADED; } catch (Throwable t) { Log.w(TAG, "download error", t); return STATE_ERROR; } } }
From source file:com.owncloud.android.ui.helpers.UriUploader.java
public UriUploaderResultCode uploadUris() { try {//from ww w. j ava 2 s .c o m List<Uri> contentUris = new ArrayList<>(); List<String> contentRemotePaths = new ArrayList<>(); int schemeFileCounter = 0; for (Parcelable sourceStream : mUrisToUpload) { Uri sourceUri = (Uri) sourceStream; if (sourceUri != null) { String displayName = UriUtils.getDisplayNameForUri(sourceUri, mActivity); if (displayName == null) { displayName = generateDiplayName(); } String remotePath = mUploadPath + displayName; if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) { contentUris.add(sourceUri); contentRemotePaths.add(remotePath); } else if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) { /// file: uris should point to a local file, should be safe let FileUploader handle them requestUpload(sourceUri.getPath(), remotePath); schemeFileCounter++; } } } if (!contentUris.isEmpty()) { /// content: uris will be copied to temporary files before calling {@link FileUploader} copyThenUpload(contentUris.toArray(new Uri[contentUris.size()]), contentRemotePaths.toArray(new String[contentRemotePaths.size()])); } else if (schemeFileCounter == 0) { mCode = UriUploaderResultCode.ERROR_NO_FILE_TO_UPLOAD; } } catch (SecurityException e) { mCode = UriUploaderResultCode.ERROR_READ_PERMISSION_NOT_GRANTED; Log_OC.e(TAG, "Permissions fail", e); } catch (Exception e) { mCode = UriUploaderResultCode.ERROR_UNKNOWN; Log_OC.e(TAG, "Unexpected error", e); } return mCode; }
From source file:com.cooliris.media.UriTexture.java
public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY, long cacheId, ClientConnectionManager connectionManager) throws IOException, URISyntaxException, OutOfMemoryError { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false;/*from ww w. jav a2 s . c o m*/ options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = true; long crc64 = 0; Bitmap bitmap = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) { // We need the filepath for the given content uri crc64 = cacheId; } else { crc64 = Utils.Crc64Long(uri); } bitmap = createFromCache(crc64, maxResolutionX); if (bitmap != null) { return bitmap; } final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://"); // Get the input stream for computing the sample size. BufferedInputStream bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Compute the sample size, i.e., not decoding real pixels. if (bufferedInput != null) { options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY); } else { return null; } // Get the input stream again for decoding it to a bitmap. bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Decode bufferedInput to a bitmap. if (bufferedInput != null) { options.inDither = false; options.inJustDecodeBounds = false; options.inPurgeable = true; Thread timeoutThread = new Thread("BitmapTimeoutThread") { public void run() { try { Thread.sleep(6000); options.requestCancelDecode(); } catch (InterruptedException e) { } } }; timeoutThread.start(); bitmap = BitmapFactory.decodeStream(bufferedInput, null, options); } if (bitmap != null) { bitmap = Utils.resizeBitmap(bitmap, maxResolutionX); } if ((options.inSampleSize > 1 || !local) && bitmap != null) { writeToCache(crc64, bitmap, maxResolutionX); } return bitmap; }
From source file:com.xlauncher.media.UriTexture.java
public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY, long cacheId, ClientConnectionManager connectionManager) throws IOException, URISyntaxException, OutOfMemoryError { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false;//from w w w. jav a 2s. c om options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = true; long crc64 = 0; Bitmap bitmap = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) { // We need the filepath for the given content uri crc64 = cacheId; } else { crc64 = Utils.Crc64Long(uri); } bitmap = createFromCache(crc64, maxResolutionX); if (bitmap != null) { return bitmap; } final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://"); // Get the input stream for computing the sample size. BufferedInputStream bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Compute the sample size, i.e., not decoding real pixels. if (bufferedInput != null) { options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY); } else { return null; } // Get the input stream again for decoding it to a bitmap. bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Decode bufferedInput to a bitmap. if (bufferedInput != null) { options.inDither = false; options.inJustDecodeBounds = false; options.inPurgeable = true; Thread timeoutThread = new Thread("BitmapTimeoutThread") { public void run() { try { Thread.sleep(6000); options.requestCancelDecode(); } catch (InterruptedException e) { } } }; timeoutThread.start(); bitmap = BitmapFactory.decodeStream(bufferedInput, null, options); } if (bitmap != null) { bitmap = Utils.resizeBitmap(context, bitmap, maxResolutionX, false); } if ((options.inSampleSize > 1 || !local) && bitmap != null) { writeToCache(crc64, bitmap, maxResolutionX); } return bitmap; }
From source file:com.timtory.wmgallery.media.UriTexture.java
public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY, long cacheId, ClientConnectionManager connectionManager) throws IOException, URISyntaxException, OutOfMemoryError { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false;//from w w w . j a v a 2 s .c o m options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = true; long crc64 = 0; Bitmap bitmap = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) { // We need the filepath for the given content uri crc64 = cacheId; } else { crc64 = Utils.Crc64Long(uri); } bitmap = createFromCache(crc64, maxResolutionX); if (bitmap != null) { return bitmap; } final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://"); // Get the input stream for computing the sample size. BufferedInputStream bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Compute the sample size, i.e., not decoding real pixels. if (bufferedInput != null) { options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY); } else { return null; } // Get the input stream again for decoding it to a bitmap. bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Decode bufferedInput to a bitmap. if (bufferedInput != null) { options.inDither = false; options.inJustDecodeBounds = false; options.inPurgeable = true; Thread timeoutThread = new Thread("BitmapTimeoutThread") { public void run() { try { Thread.sleep(6000); options.requestCancelDecode(); } catch (InterruptedException e) { } } }; timeoutThread.start(); bitmap = BitmapFactory.decodeStream(bufferedInput, null, options); } if (bitmap != null) { bitmap = Utils.resizeBitmap(context, bitmap, maxResolutionX); } if ((options.inSampleSize > 1 || !local) && bitmap != null) { writeToCache(crc64, bitmap, maxResolutionX); } return bitmap; }
From source file:com.cooliris.mediayemaha.UriTexture.java
public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY, long cacheId, ClientConnectionManager connectionManager) throws IOException, URISyntaxException, OutOfMemoryError { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false;/* ww w. j a va2 s . c o m*/ options.inPreferredConfig = Bitmap.Config.RGB_565; options.inDither = true; long crc64 = 0; Bitmap bitmap = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) { // We need the filepath for the given content uri crc64 = cacheId; } else { crc64 = Utils.Crc64Long(uri); } bitmap = createFromCache(crc64, maxResolutionX); if (bitmap != null) { return bitmap; } final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://"); // Get the input stream for computing the sample size. BufferedInputStream bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Compute the sample size, i.e., not decoding real pixels. if (bufferedInput != null) { options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY); } else { return null; } // Get the input stream again for decoding it to a bitmap. bufferedInput = null; if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) { // Get the stream from a local file. bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)), 16384); } else { // Get the stream from a remote URL. bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager); } // Decode bufferedInput to a bitmap. if (bufferedInput != null) { options.inDither = false; options.inJustDecodeBounds = false; Thread timeoutThread = new Thread("BitmapTimeoutThread") { public void run() { try { Thread.sleep(6000); options.requestCancelDecode(); } catch (InterruptedException e) { } } }; timeoutThread.start(); bitmap = BitmapFactory.decodeStream(bufferedInput, null, options); } if ((options.inSampleSize > 1 || !local) && bitmap != null) { writeToCache(crc64, bitmap, maxResolutionX / options.inSampleSize); } return bitmap; }