android.provider.OpenableColumns Java Examples

The following examples show how to use android.provider.OpenableColumns. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalUriPayload.java    From cloudinary_android with MIT License 6 votes vote down vote up
private long fetchFileSizeFromUri(Context context) {
    Cursor returnCursor = null;
    long size = 0;

    try {
        returnCursor = context.getContentResolver().query(data, PROJECTION, null, null, null);
        if (returnCursor != null && returnCursor.moveToNext()) {
            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
            size = returnCursor.getLong(sizeIndex);
        }
    } finally {
        if (returnCursor != null) {
            returnCursor.close();
        }
    }

    return size;
}
 
Example #2
Source File: Utility.java    From kognitivo with Apache License 2.0 6 votes vote down vote up
public static long getContentSize(final Uri contentUri) {
    Cursor cursor = null;
    try {
        cursor = FacebookSdk
                .getApplicationContext()
                .getContentResolver()
                .query(contentUri, null, null, null, null);
        int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);

        cursor.moveToFirst();
        return cursor.getLong(sizeIndex);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example #3
Source File: FieldEditorActivity.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #4
Source File: Utils.java    From NXLoader with MIT License 6 votes vote down vote up
public static String getFileName(Context ctx, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = ctx.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #5
Source File: FileManager.java    From PDFCreatorAndroid with MIT License 6 votes vote down vote up
public String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #6
Source File: Utility.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get a filename.
 *
 * @param u
 *   The uri
 * @param context
 *   The current context
 *
 * @return string
 */
public static String getFilename(Uri u, Context context) {
    String filename = "";

    try {
        Cursor returnCursor = context.getContentResolver().query(u, null, null, null, null);
        if (returnCursor != null) {
            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            returnCursor.moveToFirst();
            filename = returnCursor.getString(nameIndex);
            returnCursor.close();
        }
    }
    catch (NullPointerException ignored) {}

    return filename;
}
 
Example #7
Source File: MediaRepository.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private Media getContentResolverPopulatedMedia(@NonNull Context context, @NonNull Media media) throws IOException {
  int  width  = media.getWidth();
  int  height = media.getHeight();
  long size   = media.getSize();

  if (size <= 0) {
    try (Cursor cursor = context.getContentResolver().query(media.getUri(), null, null, null, null)) {
      if (cursor != null && cursor.moveToFirst() && cursor.getColumnIndex(OpenableColumns.SIZE) >= 0) {
        size = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
      }
    }
  }

  if (size <= 0) {
    size = MediaUtil.getMediaSize(context, media.getUri());
  }

  if (width == 0 || height == 0) {
    Pair<Integer, Integer> dimens = MediaUtil.getDimensions(context, media.getMimeType(), media.getUri());
    width  = dimens.first;
    height = dimens.second;
  }

  return new Media(media.getUri(), media.getMimeType(), media.getDate(), width, height, size, 0, media.getBucketId(), media.getCaption(), Optional.absent());
}
 
Example #8
Source File: DfuFilePickerFragment.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
private String getFilenameFromUri(@NonNull Context context, @Nullable Uri uri) {
    String result = null;

    // Based on: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
    if (uri != null) {
        String scheme = uri.getScheme();
        if (scheme != null && scheme.equals("content")) {
            try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
                if (cursor != null && cursor.moveToFirst()) {
                    result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                }
            }
        }
        if (result == null) {
            result = uri.getPath();
            if (result != null) {
                int cut = result.lastIndexOf('/');
                if (cut != -1) {
                    result = result.substring(cut + 1);
                }
            }
        }
    }

    return result;
}
 
Example #9
Source File: FileUtils.java    From FileTransfer with GNU General Public License v3.0 6 votes vote down vote up
public static String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #10
Source File: ContentResolverFs.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
public static String getFileNameFromCursor(Cursor cursor)
{
	if (cursor.moveToFirst())
	{
		int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
		if(columnIndex >= 0)
			return cursor.getString(columnIndex);
		else
		{
			columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);//Instead of "_data"
			if (columnIndex >= 0)
			{
				Uri filePathUri = Uri.parse(cursor.getString(columnIndex));
				return filePathUri.getLastPathSegment();
			}
		}
	}
	return null;
}
 
Example #11
Source File: DeviceServicesActivity.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #12
Source File: FileUtil.java    From JD-Test with Apache License 2.0 6 votes vote down vote up
static String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #13
Source File: FileUtil.java    From Matisse with Apache License 2.0 6 votes vote down vote up
/**
 * 获取文件名称
 * @param context   上下文
 * @param uri       uri
 * @return          文件名称
 */
static String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #14
Source File: PDFViewActivity.java    From AndroidPdfViewerV2 with Apache License 2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getLastPathSegment();
    }
    return result;
}
 
Example #15
Source File: KeyEditActivity.java    From SSLSocks with MIT License 6 votes vote down vote up
private String getFileName(Uri uri) {
	String result = null;
	if ("content".equals(uri.getScheme())) {
		try (Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
			if (cursor != null && cursor.moveToFirst()) {
				result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
			}
		}
	}
	if (result == null) {
		result = Objects.requireNonNull(uri.getPath());
		int cut = result.lastIndexOf('/');
		if (cut != -1) {
			result = result.substring(cut + 1);
		}
	}
	return result;
}
 
Example #16
Source File: MediaController.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public static String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        try (Cursor cursor = ApplicationLoader.applicationContext.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
            if (cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #17
Source File: TraitEditorActivity.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #18
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static String retrieveFileName(Context context, Uri uri) {
    //retrieve file name
    try {
        Cursor cursor = context.getContentResolver().query(uri,
                new String[]{OpenableColumns.DISPLAY_NAME},
                null, null, null);
        if (cursor != null) {
            int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            if (!cursor.isAfterLast()) {
                String filename = cursor.getString(nameIndex);
                cursor.close();
                return filename;
            }
        }
    } catch (SecurityException ignored) {
    }
    return null;
}
 
Example #19
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static InfoItem retrieveFileSize(Context context, Uri uri) {
    //retrieve fileSize form MediaStore
    Cursor cursor = context.getContentResolver().query(
            uri, null, null,
            null, null);
    long size = 0;
    if (cursor != null && !cursor.isAfterLast()) {
        int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            size = cursor.getLong(sizeIndex);
            cursor.close();
        }
    }
    return new InfoItem(context.getString(R.string.info_size), Parser.parseFileSize(context, size));
}
 
Example #20
Source File: FileUtils.java    From OsmGo with MIT License 6 votes vote down vote up
private static String getCopyFilePath(Uri uri, Context context) {
  Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
  int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
  cursor.moveToFirst();
  String name = (cursor.getString(nameIndex));
  File file = new File(context.getFilesDir(), name);
  try {
    InputStream inputStream = context.getContentResolver().openInputStream(uri);
    FileOutputStream outputStream = new FileOutputStream(file);
    int read = 0;
    int maxBufferSize = 1024 * 1024;
    int bufferSize = Math.min(inputStream.available(), maxBufferSize);
    final byte[] buffers = new byte[bufferSize];
    while ((read = inputStream.read(buffers)) != -1) {
      outputStream.write(buffers, 0, read);
    }
    inputStream.close();
    outputStream.close();
  } catch (Exception e) {
    return null;
  } finally {
    if (cursor != null)
      cursor.close();
  }
  return file.getPath();
}
 
Example #21
Source File: MediaController.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public static String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        try (Cursor cursor = ApplicationLoader.applicationContext.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
            if (cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #22
Source File: FileUtil.java    From HaiNaBaiChuan with Apache License 2.0 6 votes vote down vote up
static String getFileName(Context context, Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #23
Source File: QiscusFileUtil.java    From qiscus-sdk-android with Apache License 2.0 6 votes vote down vote up
public static String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = QiscusCore.getApps().getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}
 
Example #24
Source File: PDFViewActivity.java    From AndroidPdfViewerV1 with Apache License 2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getLastPathSegment();
    }
    return result;
}
 
Example #25
Source File: PDFViewActivity.java    From AndroidPdfViewer with Apache License 2.0 6 votes vote down vote up
public String getFileName(Uri uri) {
    String result = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getLastPathSegment();
    }
    return result;
}
 
Example #26
Source File: RingtonePreferenceDialogFragmentCompat.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
private static String getFileDisplayNameFromUri(Context context, Uri uri) {
  String scheme = uri.getScheme();

  if (ContentResolver.SCHEME_FILE.equals(scheme)) {
    return uri.getLastPathSegment();
  } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {

    String[] projection = {OpenableColumns.DISPLAY_NAME};

    Cursor cursor = null;
    try {
      cursor = context.getContentResolver().query(uri, projection, null, null, null);
      if (cursor != null && cursor.moveToFirst()) {
        return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }
  }

  // This will only happen if the Uri isn't either SCHEME_CONTENT or SCHEME_FILE, so we assume
  // it already represents the file's name.
  return uri.toString();
}
 
Example #27
Source File: UriUtils.java    From Jockey with Apache License 2.0 6 votes vote down vote up
public static String getDisplayName(Context context, Uri uri) {
    Cursor cursor = null;

    try {
        cursor = context.getContentResolver().query(uri, NAME_PROJECTION, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int nameColumn = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            String name = cursor.getString(nameColumn);

            if (name.trim().isEmpty()) {
                return getFileName(uri);
            } else {
                return name;
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return getFileName(uri);
}
 
Example #28
Source File: FileUtils.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static String getSize(Context context, Uri uri) {

        String sizeInMB = null;
        Cursor returnCursor =
                context.getContentResolver().query(uri, null, null, null, null);

        if (returnCursor != null && returnCursor.moveToFirst()) {

            int columnIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
            Long fileSize = returnCursor.getLong(columnIndex);
            if (fileSize < 1024) {
                sizeInMB = (int) (fileSize / (1024 * 1024)) + " B";

            } else if (fileSize < 1024 * 1024) {
                sizeInMB = (int) (fileSize / (1024)) + " KB";
            } else {
                sizeInMB = (int) (fileSize / (1024 * 1024)) + " MB";
            }
        }

        return sizeInMB;
    }
 
Example #29
Source File: FileUtils.java    From linphone-android with GNU General Public License v3.0 6 votes vote down vote up
private static String getNameFromUri(Uri uri, Context context) {
    String name = null;
    if (uri != null) {
        if (uri.getScheme().equals("content")) {
            Cursor returnCursor =
                    context.getContentResolver().query(uri, null, null, null, null);
            if (returnCursor != null) {
                returnCursor.moveToFirst();
                int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                name = returnCursor.getString(nameIndex);
                returnCursor.close();
            }
        } else if (uri.getScheme().equals("file")) {
            name = uri.getLastPathSegment();
        }
    }
    return name;
}
 
Example #30
Source File: FilePath.java    From 4pdaClient-plus with Apache License 2.0 6 votes vote down vote up
public static String getFileName(@NonNull Context context, Uri uri) {
    String mimeType = context.getContentResolver().getType(uri);
    String filename = null;

    if (mimeType == null && context != null) {
        String path = getPath(context, uri);
        if (path == null) {
            filename = getName(uri.toString());
        } else {
            File file = new File(path);
            filename = file.getName();
        }
    } else {
        Cursor returnCursor = context.getContentResolver().query(uri, null,
                null, null, null);
        if (returnCursor != null) {
            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            returnCursor.moveToFirst();
            filename = returnCursor.getString(nameIndex);
            returnCursor.close();
        }
    }

    return filename;
}