List of usage examples for android.provider OpenableColumns DISPLAY_NAME
String DISPLAY_NAME
To view the source code for android.provider OpenableColumns DISPLAY_NAME.
Click Source Link
From source file:org.exoplatform.utils.ExoDocumentUtils.java
/** * Gets a DocumentInfo with info coming from the document at the given URI. * //from w w w. j a va2 s . c om * @param contentUri the content URI of the document (content:// ...) * @param context * @return a DocumentInfo or null if an error occurs */ public static DocumentInfo documentFromContentUri(Uri contentUri, Context context) { if (contentUri == null) return null; try { ContentResolver cr = context.getContentResolver(); Cursor c = cr.query(contentUri, null, null, null, null); int sizeIndex = c.getColumnIndex(OpenableColumns.SIZE); int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME); int orientIndex = c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION); c.moveToFirst(); DocumentInfo document = new DocumentInfo(); document.documentName = c.getString(nameIndex); document.documentSizeKb = c.getLong(sizeIndex) / 1024; document.documentData = cr.openInputStream(contentUri); document.documentMimeType = cr.getType(contentUri); if (orientIndex != -1) { // if found orientation column document.orientationAngle = c.getInt(orientIndex); } return document; } catch (FileNotFoundException e) { Log.d(LOG_TAG, e.getClass().getSimpleName(), e.getLocalizedMessage()); } catch (Exception e) { Log.e(LOG_TAG, "Cannot retrieve the content at " + contentUri); if (Log.LOGD) Log.d(LOG_TAG, e.getMessage() + "\n" + Log.getStackTraceString(e)); } return null; }
From source file:im.vector.activity.RoomActivity.java
/** * Send a list of images from their URIs * @param mediaUris the media URIs/*from w ww. ja v a2 s .c om*/ */ private void sendMedias(final ArrayList<Uri> mediaUris) { final View progressBackground = findViewById(R.id.medias_processing_progress_background); final View progress = findViewById(R.id.medias_processing_progress); progressBackground.setVisibility(View.VISIBLE); progress.setVisibility(View.VISIBLE); final HandlerThread handlerThread = new HandlerThread("MediasEncodingThread"); handlerThread.start(); final android.os.Handler handler = new android.os.Handler(handlerThread.getLooper()); Runnable r = new Runnable() { @Override public void run() { handler.post(new Runnable() { public void run() { final int mediaCount = mediaUris.size(); for (Uri anUri : mediaUris) { // crash from Google Analytics : null URI on a nexus 5 if (null != anUri) { final Uri mediaUri = anUri; String filename = null; if (mediaUri.toString().startsWith("content://")) { Cursor cursor = null; try { cursor = RoomActivity.this.getContentResolver().query(mediaUri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { filename = cursor .getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } catch (Exception e) { Log.e(LOG_TAG, "cursor.getString " + e.getMessage()); } finally { cursor.close(); } if (TextUtils.isEmpty(filename)) { List uriPath = mediaUri.getPathSegments(); filename = (String) uriPath.get(uriPath.size() - 1); } } else if (mediaUri.toString().startsWith("file://")) { // try to retrieve the filename from the file url. try { filename = anUri.getLastPathSegment(); } catch (Exception e) { } if (TextUtils.isEmpty(filename)) { filename = null; } } final String fFilename = filename; ResourceUtils.Resource resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); if (null == resource) { RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { handlerThread.quit(); progressBackground.setVisibility(View.GONE); progress.setVisibility(View.GONE); Toast.makeText(RoomActivity.this, getString(R.string.message_failed_to_upload), Toast.LENGTH_LONG) .show(); } ; }); return; } // save the file in the filesystem String mediaUrl = mMediasCache.saveMedia(resource.contentStream, null, resource.mimeType); String mimeType = resource.mimeType; Boolean isManaged = false; if ((null != resource.mimeType) && resource.mimeType.startsWith("image/")) { // manage except if there is an error isManaged = true; // try to retrieve the gallery thumbnail // if the image comes from the gallery.. Bitmap thumbnailBitmap = null; try { ContentResolver resolver = getContentResolver(); List uriPath = mediaUri.getPathSegments(); long imageId = -1; String lastSegment = (String) uriPath.get(uriPath.size() - 1); // > Kitkat if (lastSegment.startsWith("image:")) { lastSegment = lastSegment.substring("image:".length()); } imageId = Long.parseLong(lastSegment); thumbnailBitmap = MediaStore.Images.Thumbnails.getThumbnail(resolver, imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); } catch (Exception e) { Log.e(LOG_TAG, "MediaStore.Images.Thumbnails.getThumbnail " + e.getMessage()); } double thumbnailWidth = mConsoleMessageListFragment.getMaxThumbnailWith(); double thumbnailHeight = mConsoleMessageListFragment.getMaxThumbnailHeight(); // no thumbnail has been found or the mimetype is unknown if ((null == thumbnailBitmap) || (thumbnailBitmap.getHeight() > thumbnailHeight) || (thumbnailBitmap.getWidth() > thumbnailWidth)) { // need to decompress the high res image BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); // get the full size bitmap Bitmap fullSizeBitmap = null; if (null == thumbnailBitmap) { fullSizeBitmap = BitmapFactory.decodeStream(resource.contentStream, null, options); } if ((fullSizeBitmap != null) || (thumbnailBitmap != null)) { double imageWidth; double imageHeight; if (null == thumbnailBitmap) { imageWidth = fullSizeBitmap.getWidth(); imageHeight = fullSizeBitmap.getHeight(); } else { imageWidth = thumbnailBitmap.getWidth(); imageHeight = thumbnailBitmap.getHeight(); } if (imageWidth > imageHeight) { thumbnailHeight = thumbnailWidth * imageHeight / imageWidth; } else { thumbnailWidth = thumbnailHeight * imageWidth / imageHeight; } try { thumbnailBitmap = Bitmap.createScaledBitmap( (null == fullSizeBitmap) ? thumbnailBitmap : fullSizeBitmap, (int) thumbnailWidth, (int) thumbnailHeight, false); } catch (OutOfMemoryError ex) { Log.e(LOG_TAG, "Bitmap.createScaledBitmap " + ex.getMessage()); } } // the valid mimetype is not provided if ("image/*".equals(mimeType)) { // make a jpg snapshot. mimeType = null; } // unknown mimetype if ((null == mimeType) || (mimeType.startsWith("image/"))) { try { // try again if (null == fullSizeBitmap) { System.gc(); fullSizeBitmap = BitmapFactory .decodeStream(resource.contentStream, null, options); } if (null != fullSizeBitmap) { Uri uri = Uri.parse(mediaUrl); if (null == mimeType) { // the images are save in jpeg format mimeType = "image/jpeg"; } resource.contentStream.close(); resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); try { mMediasCache.saveMedia(resource.contentStream, uri.getPath(), mimeType); } catch (OutOfMemoryError ex) { Log.e(LOG_TAG, "mMediasCache.saveMedia" + ex.getMessage()); } } else { isManaged = false; } resource.contentStream.close(); } catch (Exception e) { isManaged = false; Log.e(LOG_TAG, "sendMedias " + e.getMessage()); } } // reduce the memory consumption if (null != fullSizeBitmap) { fullSizeBitmap.recycle(); System.gc(); } } String thumbnailURL = mMediasCache.saveBitmap(thumbnailBitmap, null); if (null != thumbnailBitmap) { thumbnailBitmap.recycle(); } // if (("image/jpg".equals(mimeType) || "image/jpeg".equals(mimeType)) && (null != mediaUrl)) { Uri imageUri = Uri.parse(mediaUrl); // get the exif rotation angle final int rotationAngle = ImageUtils .getRotationAngleForBitmap(RoomActivity.this, imageUri); if (0 != rotationAngle) { // always apply the rotation to the image ImageUtils.rotateImage(RoomActivity.this, thumbnailURL, rotationAngle, mMediasCache); // the high res media orientation should be not be done on uploading //ImageUtils.rotateImage(RoomActivity.this, mediaUrl, rotationAngle, mMediasCache)) } } // is the image content valid ? if (isManaged && (null != thumbnailURL)) { final String fThumbnailURL = thumbnailURL; final String fMediaUrl = mediaUrl; final String fMimeType = mimeType; RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // if there is only one image if (mediaCount == 1) { // display an image preview before sending it mPendingThumbnailUrl = fThumbnailURL; mPendingMediaUrl = fMediaUrl; mPendingMimeType = fMimeType; mPendingFilename = fFilename; mConsoleMessageListFragment.scrollToBottom(); manageSendMoreButtons(); } else { mConsoleMessageListFragment.uploadImageContent(fThumbnailURL, fMediaUrl, fFilename, fMimeType); } } }); } } // default behaviour if ((!isManaged) && (null != mediaUrl)) { final String fMediaUrl = mediaUrl; final String fMimeType = mimeType; RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { mConsoleMessageListFragment.uploadMediaContent(fMediaUrl, fMimeType, fFilename); } }); } } } RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { handlerThread.quit(); progressBackground.setVisibility(View.GONE); progress.setVisibility(View.GONE); }; }); } }); } }; Thread t = new Thread(r); t.start(); }
From source file:cgeo.geocaching.CacheListActivity.java
private String getDisplayName(final Uri uri) { Cursor cursor = null;//from w w w .j ava 2 s.c om try { cursor = getContentResolver().query(uri, new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null); if (cursor != null && cursor.moveToFirst()) { return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } finally { if (cursor != null) { cursor.close(); // no Closable Cursor below sdk 16 } } return null; }
From source file:org.matrix.console.activity.RoomActivity.java
/** * Send a list of images from their URIs * @param mediaUris the media URIs/*from w w w .ja va2s. com*/ */ private void sendMedias(final ArrayList<Uri> mediaUris) { final View progressBackground = findViewById(R.id.medias_processing_progress_background); final View progress = findViewById(R.id.medias_processing_progress); progressBackground.setVisibility(View.VISIBLE); progress.setVisibility(View.VISIBLE); final HandlerThread handlerThread = new HandlerThread("MediasEncodingThread"); handlerThread.start(); final android.os.Handler handler = new android.os.Handler(handlerThread.getLooper()); Runnable r = new Runnable() { @Override public void run() { handler.post(new Runnable() { public void run() { final int mediaCount = mediaUris.size(); for (Uri anUri : mediaUris) { // crash from Google Analytics : null URI on a nexus 5 if (null != anUri) { final Uri mediaUri = anUri; String filename = null; if (mediaUri.toString().startsWith("content://")) { Cursor cursor = null; try { cursor = RoomActivity.this.getContentResolver().query(mediaUri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { filename = cursor .getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } catch (Exception e) { Log.e(LOG_TAG, "cursor.getString " + e.getMessage()); } finally { if (null != cursor) { cursor.close(); } } if (TextUtils.isEmpty(filename)) { List uriPath = mediaUri.getPathSegments(); filename = (String) uriPath.get(uriPath.size() - 1); } } else if (mediaUri.toString().startsWith("file://")) { // try to retrieve the filename from the file url. try { filename = anUri.getLastPathSegment(); } catch (Exception e) { } if (TextUtils.isEmpty(filename)) { filename = null; } } final String fFilename = filename; ResourceUtils.Resource resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); if (null == resource) { RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { handlerThread.quit(); progressBackground.setVisibility(View.GONE); progress.setVisibility(View.GONE); Toast.makeText(RoomActivity.this, getString(R.string.message_failed_to_upload), Toast.LENGTH_LONG) .show(); } ; }); return; } // save the file in the filesystem String mediaUrl = mMediasCache.saveMedia(resource.contentStream, null, resource.mimeType); String mimeType = resource.mimeType; Boolean isManaged = false; if ((null != resource.mimeType) && resource.mimeType.startsWith("image/")) { // manage except if there is an error isManaged = true; // try to retrieve the gallery thumbnail // if the image comes from the gallery.. Bitmap thumbnailBitmap = null; try { ContentResolver resolver = getContentResolver(); List uriPath = mediaUri.getPathSegments(); long imageId = -1; String lastSegment = (String) uriPath.get(uriPath.size() - 1); // > Kitkat if (lastSegment.startsWith("image:")) { lastSegment = lastSegment.substring("image:".length()); } imageId = Long.parseLong(lastSegment); thumbnailBitmap = MediaStore.Images.Thumbnails.getThumbnail(resolver, imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); } catch (Exception e) { Log.e(LOG_TAG, "MediaStore.Images.Thumbnails.getThumbnail " + e.getMessage()); } double thumbnailWidth = mConsoleMessageListFragment.getMaxThumbnailWith(); double thumbnailHeight = mConsoleMessageListFragment.getMaxThumbnailHeight(); // no thumbnail has been found or the mimetype is unknown if ((null == thumbnailBitmap) || (thumbnailBitmap.getHeight() > thumbnailHeight) || (thumbnailBitmap.getWidth() > thumbnailWidth)) { // need to decompress the high res image BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); // get the full size bitmap Bitmap fullSizeBitmap = null; if (null == thumbnailBitmap) { fullSizeBitmap = BitmapFactory.decodeStream(resource.contentStream, null, options); } if ((fullSizeBitmap != null) || (thumbnailBitmap != null)) { double imageWidth; double imageHeight; if (null == thumbnailBitmap) { imageWidth = fullSizeBitmap.getWidth(); imageHeight = fullSizeBitmap.getHeight(); } else { imageWidth = thumbnailBitmap.getWidth(); imageHeight = thumbnailBitmap.getHeight(); } if (imageWidth > imageHeight) { thumbnailHeight = thumbnailWidth * imageHeight / imageWidth; } else { thumbnailWidth = thumbnailHeight * imageWidth / imageHeight; } try { thumbnailBitmap = Bitmap.createScaledBitmap( (null == fullSizeBitmap) ? thumbnailBitmap : fullSizeBitmap, (int) thumbnailWidth, (int) thumbnailHeight, false); } catch (OutOfMemoryError ex) { Log.e(LOG_TAG, "Bitmap.createScaledBitmap " + ex.getMessage()); } } // the valid mimetype is not provided if ("image/*".equals(mimeType)) { // make a jpg snapshot. mimeType = null; } // unknown mimetype if ((null == mimeType) || (mimeType.startsWith("image/"))) { try { // try again if (null == fullSizeBitmap) { System.gc(); fullSizeBitmap = BitmapFactory .decodeStream(resource.contentStream, null, options); } if (null != fullSizeBitmap) { Uri uri = Uri.parse(mediaUrl); if (null == mimeType) { // the images are save in jpeg format mimeType = "image/jpeg"; } resource.contentStream.close(); resource = ResourceUtils.openResource(RoomActivity.this, mediaUri); try { mMediasCache.saveMedia(resource.contentStream, uri.getPath(), mimeType); } catch (OutOfMemoryError ex) { Log.e(LOG_TAG, "mMediasCache.saveMedia" + ex.getMessage()); } } else { isManaged = false; } resource.contentStream.close(); } catch (Exception e) { isManaged = false; Log.e(LOG_TAG, "sendMedias " + e.getMessage()); } } // reduce the memory consumption if (null != fullSizeBitmap) { fullSizeBitmap.recycle(); System.gc(); } } String thumbnailURL = mMediasCache.saveBitmap(thumbnailBitmap, null); if (null != thumbnailBitmap) { thumbnailBitmap.recycle(); } // if (("image/jpg".equals(mimeType) || "image/jpeg".equals(mimeType)) && (null != mediaUrl)) { Uri imageUri = Uri.parse(mediaUrl); // get the exif rotation angle final int rotationAngle = ImageUtils .getRotationAngleForBitmap(RoomActivity.this, imageUri); if (0 != rotationAngle) { // always apply the rotation to the image ImageUtils.rotateImage(RoomActivity.this, thumbnailURL, rotationAngle, mMediasCache); // the high res media orientation should be not be done on uploading //ImageUtils.rotateImage(RoomActivity.this, mediaUrl, rotationAngle, mMediasCache)) } } // is the image content valid ? if (isManaged && (null != thumbnailURL)) { final String fThumbnailURL = thumbnailURL; final String fMediaUrl = mediaUrl; final String fMimeType = mimeType; RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // if there is only one image if (mediaCount == 1) { // display an image preview before sending it mPendingThumbnailUrl = fThumbnailURL; mPendingMediaUrl = fMediaUrl; mPendingMimeType = fMimeType; mPendingFilename = fFilename; mConsoleMessageListFragment.scrollToBottom(); manageSendMoreButtons(); } else { mConsoleMessageListFragment.uploadImageContent(fThumbnailURL, fMediaUrl, fFilename, fMimeType); } } }); } } // default behaviour if ((!isManaged) && (null != mediaUrl)) { final String fMediaUrl = mediaUrl; final String fMimeType = mimeType; RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if ((null != fMimeType) && fMimeType.startsWith("video/")) { mConsoleMessageListFragment.uploadVideoContent(fMediaUrl, null, fMimeType); } else { mConsoleMessageListFragment.uploadFileContent(fMediaUrl, fMimeType, fFilename); } } }); } } } RoomActivity.this.runOnUiThread(new Runnable() { @Override public void run() { handlerThread.quit(); progressBackground.setVisibility(View.GONE); progress.setVisibility(View.GONE); }; }); } }); } }; Thread t = new Thread(r); t.start(); }
From source file:org.mozilla.gecko.GeckoApp.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case FILE_PICKER_REQUEST: String filePickerResult = ""; if (data != null && resultCode == RESULT_OK) { try { ContentResolver cr = getContentResolver(); Uri uri = data.getData(); Cursor cursor = GeckoApp.mAppContext.getContentResolver().query(uri, new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null); String name = null; if (cursor != null) { try { if (cursor.moveToNext()) { name = cursor.getString(0); }/*from w ww . ja v a 2 s .c o m*/ } finally { cursor.close(); } } String fileName = "tmp_"; String fileExt = null; int period; if (name == null || (period = name.lastIndexOf('.')) == -1) { String mimeType = cr.getType(uri); fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType); } else { fileExt = name.substring(period); fileName = name.substring(0, period); } File file = File.createTempFile(fileName, fileExt, sGREDir); FileOutputStream fos = new FileOutputStream(file); InputStream is = cr.openInputStream(uri); byte[] buf = new byte[4096]; int len = is.read(buf); while (len != -1) { fos.write(buf, 0, len); len = is.read(buf); } fos.close(); filePickerResult = file.getAbsolutePath(); } catch (Exception e) { Log.e(LOGTAG, "showing file picker", e); } } try { mFilePickerResult.put(filePickerResult); } catch (InterruptedException e) { Log.i(LOGTAG, "error returning file picker result", e); } break; case AWESOMEBAR_REQUEST: if (data != null) { String url = data.getStringExtra(AwesomeBar.URL_KEY); AwesomeBar.Type type = AwesomeBar.Type.valueOf(data.getStringExtra(AwesomeBar.TYPE_KEY)); String searchEngine = data.getStringExtra(AwesomeBar.SEARCH_KEY); boolean userEntered = data.getBooleanExtra(AwesomeBar.USER_ENTERED_KEY, false); if (url != null && url.length() > 0) loadRequest(url, type, searchEngine, userEntered); } break; case CAMERA_CAPTURE_REQUEST: Log.i(LOGTAG, "Returning from CAMERA_CAPTURE_REQUEST: " + resultCode); File file = new File(Environment.getExternalStorageDirectory(), "cameraCapture-" + Integer.toString(kCaptureIndex) + ".jpg"); kCaptureIndex++; GeckoEvent e = GeckoEvent.createBroadcastEvent("cameraCaptureDone", resultCode == Activity.RESULT_OK ? "{\"ok\": true, \"path\": \"" + file.getPath() + "\" }" : "{\"ok\": false, \"path\": \"" + file.getPath() + "\" }"); GeckoAppShell.sendEventToGecko(e); break; } }