Android ImageView并不总是显示图片

时间:2015-02-08 17:30:12

标签: android android-layout

在我的应用中,我希望用户能够从图库中选择图片。然后,应在我的ImageView中显示所选图片。我找到了几个不同的例子,但是没有成功。 picasa中的某些图片将设置为Imageview,但如果我从图库中选择相同的图片,它将永远不会显示在ImageView中。我做了一些研究,我看到有些人能够通过改变布局或使用BitmapFactory缩放图片来解决这个问题。我尝试过这两种方法,但这些对我来说都没有用。

请注意,在我的AVD上,一切顺利。在真实设备上,它或者在选择图片时崩溃,或者在没有任何图片的情况下显示图像视图。 这是我的代码:

private View.OnClickListener onUploadImage = new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Picture"),
                SELECT_PICTURE);
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            uploadImage.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
        android:background="#E6C4A8"
        android:orientation="vertical" 
tools:context="com.example.weddingplanner.MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <ImageView
    android:id="@+id/settingsBanner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:scaleType="fitXY"
    android:src="@drawable/settingsbanner" />

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >

          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="fill_horizontal"
              android:orientation="vertical"
              android:gravity="center"
              android:paddingLeft="10dp"
              android:paddingRight="10dp"
              android:paddingTop="10dp" >

              <com.example.weddingplanner.MyTextView
                  android:id="@+id/coupleNameTextView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#000818"
                  android:gravity="center"
                  android:text="@string/couple_name"
                  android:textColor="#E6C4A8"
                  android:textSize="28sp"
                  customfontdemo:fontName="arabtype.ttf" />

              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal" >

                  <com.example.weddingplanner.MyTextView
                        android:id="@+id/welcomeLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/names"
                        android:textSize="22sp"
                        customfontdemo:fontName="arabtype.ttf"
                        android:textColor="#000000" />

                  <EditText
                      android:id="@+id/namesTextbox"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:ems="10"
                      android:inputType="textPersonName" />
              </LinearLayout>

              <com.example.weddingplanner.MyTextView
                  android:id="@+id/weddingDateLabel"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#000818"
                  android:gravity="center"
                  android:text="@string/wedding_date"
                  android:textColor="#E6C4A8"
                  android:textSize="28sp"
                  customfontdemo:fontName="arabtype.ttf" />

              <LinearLayout
                  android:id="@+id/welcomeLabel"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal" >

                  <com.example.weddingplanner.MyTextView
                        android:id="@+id/dateTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/date"
                        android:textSize="22sp"
                        customfontdemo:fontName="arabtype.ttf"
                        android:textColor="#000000" />

                  <EditText
                      android:id="@+id/dateTextbox"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:ems="10"
                      android:inputType="date" >

                      <requestFocus />
                  </EditText>
              </LinearLayout>
          </LinearLayout>

          <ImageView
              android:id="@+id/uploadImage"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:adjustViewBounds="true"
              android:scaleType="fitXY"
              android:layout_weight="0.49"
              android:src="@drawable/uploadpicturebutton" />

          <LinearLayout
              android:layout_width="match_parent"
              android:gravity="center"
              android:layout_height="wrap_content" >

              <ImageButton
                  android:id="@+id/saveButton"
                  android:layout_width="wrap_content"
                  android:scaleType="fitXY"
                  android:background="#E6C4A8"
                  android:paddingRight="5dp"
                  android:layout_height="wrap_content"
                  android:src="@drawable/savebutton" />

              <ImageButton
                  android:id="@+id/clearButton"
                  android:layout_width="wrap_content"
                  android:scaleType="fitXY"
                  android:background="#E6C4A8"
                  android:paddingLeft="5dp"
                  android:layout_height="wrap_content"
                  android:src="@drawable/clearbutton" />

          </LinearLayout>

      </LinearLayout>

</LinearLayout>


</LinearLayout>

3 个答案:

答案 0 :(得分:1)

从文件路径

创建可绘制对象

例: Drawable d = Drawable.createFromPath(selectedImagePath);

uploadImage.setImageDrawable(d);

答案 1 :(得分:1)

虽然如果没有关于例外的更多细节,很难确切地说出问题是什么,但我想指出你的方法存在的固有问题。

您尝试将完整的原始图像显示在UI中。我不知道这些图像有多大,但要记住应用程序的内存限制。如果您尝试将8MP图像加载到内存中,则您正在使用(对于ARGB)4×8 MB = 32 MB。根据您的设备,这可能已占用整个堆,并导致OutOfMemoryException。

您提到“使用BitmapFactory”并不适合您。如果您使用BitmapFactory以较低的样本大小加载图像,这可以消除内存问题。

我会避免重新发明轮子并使用Picasso这样的库来加载图像。

答案 2 :(得分:0)

您是否在清单文件中添加了相关权限?如果没有,请添加此项并尝试:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果它仍然崩溃,请从logcat粘贴您的日志..这将有助于深入挖掘真正的问题..