android - Android 中 FilePath 的 ImageView 加载问题(截断)
问题描述
我有一个来自文件路径的屏幕截图图像,我试图将其加载到 ImageView 中。
但是,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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CostActivity">
<!-- Ar Screen capture -->
<!-- Ar Screen capture -->
<ImageView
android:id="@+id/arScreen1"
android:layout_width="match_parent"
android:layout_height="211dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
/>
<!-- Description -->
<TextView
android:id="@+id/quote2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DESCRIPTION"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/descriptionTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/descriptionInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Measurement -->
<TextView
android:id="@+id/measurement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MEASUREMENT"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Length -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/lengthTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="170dp"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/lengthInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="Length"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Breadth -->
<TextView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="X"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:textStyle="bold" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/breathTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="179dp"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/breathInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="Breadth"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<Spinner
android:id="@+id/cm"
style="@style/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:textStyle="bold"/>
</LinearLayout>
<!-- Cost Per Square CM -->
<TextView
android:id="@+id/cost_per_square_cm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COST PER SQUARE UNIT"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/costTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/costInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="$"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Variable -->
<TextView
android:id="@+id/variable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="VARIABLE (+)"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/variableTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/variableInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="$"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
我尝试了很多方法,比如减少布局重量,但都不起作用。我怀疑这也可能是由于 Java 代码。
解决方案
并尝试以下代码从文件中设置位图图像。
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" />
推荐阅读
- javascript - 如何在传单中标准化属性并动态分配 fillColor 变量
- url - AudioInputStream 正在抛出 NullPointerException
- entity-framework - 如何为多个拥有的导航属性使用相同的数据库表,这些导航属性是与 EF Core 相同类型的集合?
- triggers - 创建相关对象时如何使用触发器将复选框设置为true
- java - 没有 FXML 文件的 JavaFX Scene Builder 替代方案
- excel - VBA - 重命名文件
- python - Python:如何找到值为 n 的所有行的平均值?
- java - Talend tRunJob 返回 NoClassDefFoundError 和子作业列表
- php - PHP - 使用 array_rand 选择时停止缓存图像
- java - 关于 Java 中 Selenium 并行测试性能的问题