2013-01-05 149 views
0

我想从SD卡加载4MB,19MB,3MB大小的图像到图像视图。如何从SD卡android加载不同大小的图像?

BitmapFactory.Options options=new BitmapFactory.Options(); 
options.inJustDecodeBounds=true; 
InputStream inputStream=new BufferedInputStream(new FileInputStream(fileName)); 
options.inSampleSize=2; 
options.inJustDecodeBounds=false; 
Bitmap bmp=BitmapFactory.decodeFile(fileName,options); 

当我使用此代码时,我无法获得所有图像的确切大小。基于屏幕功能,应该加载图像。如果一个屏幕有能力加载19mb图像,那么我不想使用option.insampleSize = 2。如果没有那个,那个时候我只想为那个18Mb和其他我不想这样做的图像减小图像的大小。

+0

你可以把你的图像放在可绘制的文件夹LDPI,HDPI,MDPI等不同的屏幕尺寸。 –

+0

你不应该加载任何全分辨率,除非它小于屏幕尺寸。 – Doomsknight

+0

ali imran。我从远程路径加载这些位图 – Robin

回答

0

你可以做这样的事情

int sampleSize = 1; 

while (true) { 
    try { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 

     options.inSampleSize = sampleSize; 
     options.inJustDecodeBounds = false; 

     InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName)); 
     Bitmap bmp=BitmapFactory.decodeFile(fileName,options); 

     // some code here 

     break; 
    } catch (OutOfMemoryError oom) { 
     sampleSize++; 
    } 
} 
+0

它不工作vmironov。 – Robin

0

可以dinamically屏幕的请求大小

int screenSize = (resources.getConfiguration().screenLayout & 
     Configuration.SCREENLAYOUT_SIZE_MASK); 
DisplayMetrics metrics = resources.getDisplayMetrics(); 

然后用

metrics.widthPixels, metrics.heightPixels 

if (screenSize == Configuration.SCREENLAYOUT_SIZE_SMALL) // or another constant screen sizes in Configuration class. 

,并要求图像的大小,而无需加载使用图像的URI的整体形象:

InputStream input = contentResolver.openInputStream(uri); 
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); 
onlyBoundsOptions.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(input, null, onlyBoundsOptions); 
input.close(); 
int originalHeight = onlyBoundsOptions.outHeight; 
int originalWidth = onlyBoundsOptions.outWidth; 

,然后选择inSampleSize在运行。

+0

如果某些图像具有较小分辨率(1024 * 1024)的小尺寸(2MB),那么这是不必要的。如果图像尺寸较小,我们必须加载完整图像,并确切分辨该图像。 – Robin

+0

你可以通过InputStream获取来自URI的字节数组iStream = getContentResolver()。openInputStream(uri); byte [] inputData = getBytes(iStream);并知道位图的大小,然后选择inSampleSize – TpoM6oH

+0

poM60H。我怎样才能找到最大尺寸可以在屏幕上加载? – Robin

相关问题