content:// scheme 和 file:// scheme

水深无声 2022-06-02 06:05 509阅读 0赞

path 转 Uri (content://)

  1. /** * 根据file path 获取content:// * @param context * @param filePath * @return content:// */
  2. public static Uri getImageContentUri(Context context, String filePath) {
  3. Cursor cursor = context.getContentResolver().query(
  4. MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  5. new String[] { MediaStore.Images.Media._ID},
  6. MediaStore.Images.Media.DATA + "=? ",
  7. new String[] { filePath }, null);
  8. if (cursor != null && cursor.moveToFirst()) {
  9. int id = cursor.getInt(cursor
  10. .getColumnIndex(MediaStore.MediaColumns._ID));
  11. Uri baseUri = Uri.parse("content://media/external/file");
  12. return Uri.withAppendedPath(baseUri, "" + id);
  13. } else {
  14. File file = new File(filePath);
  15. if (file.exists()) {
  16. ContentValues values = new ContentValues();
  17. values.put(MediaStore.Images.Media.DATA, filePath);
  18. return context.getContentResolver().insert(
  19. MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  20. } else {
  21. return null;
  22. }
  23. }
  24. }

发表评论

表情:
评论列表 (有 0 条评论,509人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ios的URL Scheme

    IOS的应用程序是运行相对独立的沙盒里的,并且在后台的应用大部分处于挂起的状态,这样不同应用的交互就成了一个难题。还好苹果提供了一个URL Scheme协议,可以让不同应用互相