我无法从文件中获取Uri

时间:2014-09-23 06:57:50

标签: java android uri

我希望从文件路径中获取Uri。这是文件路径:

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

我试图用这种方式来审视uri:

Uri uri = Uri.fromFile(path);

但我也尝试制作日志:Log.i("URI", "Uri dell' img " + uri + ":"); 而uri仍然是文件路径!不是例如:“content:// ...”而是“file:// sdcard ....”

我怎样才能拥有正确的uri?感谢

3 个答案:

答案 0 :(得分:1)

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;
}

答案 1 :(得分:0)

使用方法Uri.parse(path.getAbsolutePath());此方法将为您提供文件路径的Uri

答案 2 :(得分:0)

//write to this 

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