在我的应用程序中,我希望用户能够从图库中选择图片。所选图片应显示在我的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>
当它崩溃时,什么是堆栈跟踪? – iRuth 2015-02-08 20:23:19