2015-06-10 58 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 }; 
} 
相關問題