我有一些问题从SD卡显示图像,我尝试将位图加载到imageview。
这是加载图片的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
File imagen1 = new File(ruta + contratoActual.getImgDoc1());
if (imagen1.exists()) {
try {
bmap = BitmapFactory.decodeStream(new FileInputStream(imagen1), null, options);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 100, out);
decoded[0] = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
} catch (FileNotFoundException e) {
Log.e("ERROR", e.getMessage());
}
将位图加载到图像视图中:
LayoutParams p = (LayoutParams) img1.getLayoutParams();
p.width = 250;
p.height = 190;
if (result[0] != null) {
img1.setImageBitmap(result[0]);
img1.setScaleType(ImageView.ScaleType.CENTER_CROP);
img1.setLayoutParams(p);
result[0].recycle();
}
在这里显示结果:
我有这个日志结果:
答案 0 :(得分:0)
在您的代码中尝试此操作:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap YourBitmap = BitmapFactory.decodeFile(YourImagePath, options);
文件读取权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />