2015-06-10 56 views
0

我是相当新的android和被要求改进一个应用程序。该应用程序使用保存在手机文件中的图像。我添加了一个功能,该应用程序还使用保存在可绘制文件夹中的图像。但是,下面的代码现在不起作用:使用在应用程序中绘制保存的图像 - Android

public static int[] getImageSize(Uri uri) { 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(uri.getPath(), options); 
     return new int[] { options.outWidth, options.outHeight }; 
    } 

它返回图像的大小。由于decodeFile()方法,我很确定它不工作。我怎样才能解决这个问题,使其与我的drawables一起工作呢?

+0

我们想知道您现在在uri.getPath()中使用了什么。但无论如何decodeFile()不可用,因为drwawables中没有文件系统文件。您应该改用decodeFromStream。并从drawables资源中打开一个Input streem。一点点谷歌搜索会尽快给你两条线。 – greenapps

回答

0

您将需要检查图像是否是来自该文件的位图,或者如果它是第一个可绘制的。使用此代码获取可绘制大小的新函数

public static int[] getImageSize(Drawable image) { 
    int iHeight = image.getIntrinsicHeight(); 
    int iWidth = image.getIntrinsicWidth(); 
    return new int[] { iWidth , iHeight }; 
} 
相关问题