2015-02-08 66 views
1

在我的应用程序中,我希望用户能够从图库中选择图片。所选图片应显示在我的ImageView中。我发现了几个不同的例子来说明如何做到这一点,但没有成功。一些picasa中的图片将被设置为Imageview,但是如果我从图库中选择相同的图片,它将永远不会显示在ImageView中。我做了一些研究,我发现有些人可以通过改变它们的布局来解决这个问题,或者通过使用BitmapFactory缩放图片来解决这个问题。我已经尝试了这两种方法,而这些对我来说都不起作用。Android ImageView并非总是显示图片

请注意,在我的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> 
+1

当它崩溃时,什么是堆栈跟踪? – iRuth 2015-02-08 20:23:19

回答

1

虽然这是很难说如果没有关于异常的更多细节,问题是什么,我想指出你的方法存在固有的问题。

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

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

我反而会避免重新发明轮子,并使用库如Picasso来加载图像。

+0

谢谢!我尝试过使用毕加索,它是最后一块拼图。很酷的东西! – user2401500 2015-02-08 22:58:27

1

从文件路径

防爆创建可绘制对象: 可绘制d = Drawable.createFromPath(selectedImagePath);

uploadImage.setImageDrawable(d);

+0

感谢您的回复,不幸的是,这并没有改变任何东西。 – user2401500 2015-02-08 18:10:40

0

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

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

如果它仍然崩溃,也请粘贴记录从logcat中。这将有助于更深入地了解真正的问题..

+0

我也忘了这么做,所以谢谢! – user2401500 2015-02-08 22:59:19

+0

如果这有所帮助,至少有1投票可以给,如果不接受.. :) – 2015-02-09 04:01:29

+0

我会,但我需要15名声誉:( – user2401500 2015-02-09 19:01:39