首页 > 解决方案 > 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 代码。

标签: androidxmlimageviewscreenshotfilepath

解决方案


并尝试以下代码从文件中设置位图图像。

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" />

推荐阅读