你好,我有這樣的一段代碼:BitmapFactory.decodeFile總是小蜜
編輯:
imageName = data.getData();
try{
InputStream stream = getContentResolver().openInputStream(imageName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(stream,null,options);
final int REQUIRED_WIDTH=(int)screenWidth;
final int REQUIRED_HIGHT=(int)screenHeight;
int scale=1;
while(options.outWidth/scale/2>=REQUIRED_WIDTH && options.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
bitmap = BitmapFactory.decodeStream(stream, null, o2);
if (bitmap != null){
ok=true;
}
}catch(Exception e){
Toast.makeText(getBaseContext(),"error", Toast.LENGTH_SHORT).show();
}
但位圖仍然是空
誰能告訴我爲什麼?或更好的...如何解決它?
什麼是'getPathFromURI()'?如果這是我認爲的那樣,請刪除它,因爲[Uri'不一定是'File'](http://commonsware.com/blog/2014/07/04/uri-not-necessarily -file.html)。 – CommonsWare 2014-10-02 19:05:17
我不明白。我將在這裏添加方法 – 2014-10-02 19:06:42
刪除該方法。閱讀[我鏈接到的博客文章](http://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html)。使用'ContentResolver'上的'openInputStream()'讀入'Uri'處的數據。 – CommonsWare 2014-10-02 19:09:29