Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从URI获取图像宽度和高度_Android - Fatal编程技术网

Android 从URI获取图像宽度和高度

Android 从URI获取图像宽度和高度,android,Android,是否可以从图像文件的URI获取其宽度和高度。我试图使用此代码,但出现错误: 在getAbsolutePath()之后出现语法错误 标记“)”上的语法错误,参数列表无效 private void getDropboxIMGSize(Uri uri){ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(uri.

是否可以从图像文件的URI获取其宽度和高度。我试图使用此代码,但出现错误: 在getAbsolutePath()之后出现语法错误

标记“)”上的语法错误,参数列表无效

private void getDropboxIMGSize(Uri uri){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
}
这样做

public void getDropboxIMGSize(Uri uri) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(getAbsolutePath(uri), o);
        int imageHeight = o.outHeight;
        int imageWidth = o.outWidth;
    }



public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

如果将参数提取到局部变量中,则不太可能漏掉括号/包含额外的括号,并且代码更容易阅读

之前:

private void getDropboxIMGSize(Uri uri){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
}
之后:

private void getDropboxIMGSize(Uri uri){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    String path = uri.getPath().getAbsolutePath();
    BitmapFactory.decodeFile(path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
}

注意,您已经为局部变量指定了
options.outHeight
options.outWidth
,然后方法结束;您没有对这些值执行任何操作。

我遇到了相同的问题,我在找到了解决方案。您必须使用

BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);
在您的代码中,而不是在此:

BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);

getAbsolutePath()……令牌上的语法错误“)”,无效的参数列表……您错过了(在uri.getPath()之前),内容uri可能来自不同于MediaStore的提供程序。在这种情况下,您将得到一个空路径。您决不能从
uri
实例化
文件。