简体   繁体   English

我无法从文件中获取Uri

[英]I can't get the Uri from a file

i want to have the Uri from a file path. 我想从文件路径获取Uri。 This is the file path: 这是文件路径:

File path = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"pic/picture.jpeg");

i tried in this way to retreive the uri: 我以这种方式尝试了uri:

Uri uri = Uri.fromFile(path);

but i tried also to make a log: Log.i("URI", "Uri dell' img " + uri + ":"); 但我也尝试做一个日志: Log.i("URI", "Uri dell' img " + uri + ":"); and the uri is still the file path! uri仍然是文件路径! Not for example: "content://..." but "file://sdcard...." 例如:“ content:// ...”,而不是“ file:// sdcard ....”

How can i have the correct uri? 我怎样才能得到正确的尿? Thanks 谢谢

public File getFileFromContentUri(final Uri uri) 
{
    if (uri == null || !(uri.getScheme().equals("content") || uri.toString().contains("file://"))) return null;

    if (uri.getScheme().equals("content")) {
        final String[] columns = new String[] {MediaStore.MediaColumns.DATA};
        final Cursor cursor = getContentResolver().query(uri, columns, null, null, null);
        if (cursor == null || cursor.getCount() == 0) return null;
        cursor.moveToFirst();
        final String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
        if (path != null) return new File(path);
        else return null;
   }
   else if (uri.toString().contains("file://")) {
       return new File(uri.getPath());
   }

   return null;
}

use the method Uri.parse(path.getAbsolutePath()); 使用方法Uri.parse(path.getAbsolutePath()); this method will give you the Uri of the file Path 此方法将为您提供文件路径的Uri

//write to this 

Uri photoUri = MediaStore.Images.Media.getContentUri("external");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM