Android中文件路径的ImageView加载问题(切断)
我的文件路径中有一个截图图像,我正试图将其加载到ImageView中。Android中文件路径的ImageView加载问题(切断),android,xml,imageview,screenshot,filepath,Android,Xml,Imageview,Screenshot,Filepath,我的文件路径中有一个截图图像,我正试图将其加载到ImageView中。 但是,imageView被切断 这就是它的样子: 应该是这样的: 这是我的代码: // MainActivity.java //Setting image view from filepath File imgFile = new File(arScreenshotPath); ImageView myImage = findViewById(R.id.arScreen1); if(i
但是,imageView被切断 这就是它的样子: 应该是这样的: 这是我的代码:
// MainActivity.java
//Setting image view from filepath
File imgFile = new File(arScreenshotPath);
ImageView myImage = findViewById(R.id.arScreen1);
if(imgFile.exists()){
myImage.setImageBitmap(Utils.decodeSampledBitmapFromFile(arScreenshotPath, 500, 250));
// myImage.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, 206, 144, false));
}else {
myImage.setImageResource(R.drawable.no_image);
}
XML
我尝试过很多方法,比如减轻布局重量,但都不起作用。
我怀疑这也可能是由于Java代码。在ImageView块中添加此属性
android:scaleType=“fitCenter”
android:layout\u weight=“0.2”
您可以更改android:layout\u weight的值,然后尝试以下代码从文件中设置位图图像
File imgFile = new File(arScreenshotPath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = findViewById(R.id.arScreen1);
myImage.setImageBitmap(myBitmap);
}
并将此权限包括在清单文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您可能希望节省一些时间,并使用编写良好的图像库!e、 另外,从您的代码中,我可以猜问题来自myImage.setImageBitmap。。。您还可以指定图像的宽度和高度!因此,有关imageView的xml属性会被java代码覆盖。您能显示decodeSampledBitmapFromFile是什么吗?试试,这是我的解决方案我已经尝试了上述所有方法,但仍然不起作用
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />