Reputation: 13
In my app I want the user to be able to select a picture from a gallery. The selected picture should be then displayed in my ImageView
. I found several different examples of how to do this, but with no success. Some of the pictures in picasa will be set to the Imageview
, but if i select the same picture from the gallery, it is never displayed in the ImageView
. I did some research and I have seen that some people were able to fix this by changing their layout, or by scaling their picture using the BitmapFactory. I have tried both of these, and these didn't work for me.
Please note that on my AVD everything runs smooth. On a real device it either crashes on selecting a picture, or the imageview is shown without any picture. Here is my code:
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);
}
And here's my 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>
Upvotes: 1
Views: 95
Reputation: 38292
While it's difficult to say exactly what the problem is without more details about the exception, I'd like to point out an inherent problem with your approach.
You're attempting to display the entire, raw image into your UI. I don't know how large those images images are, but keep your application's memory constraints in mind. If you're attempting to load a 8MP image into memory, you are consuming (for ARGB) 4 × 8 MB = 32 MB. Depending on your device, this may already consume the entire heap, and cause an OutOfMemoryException.
You mentioned that "using the BitmapFactory" didn't work for you. If you've used the BitmapFactory to load images in at a lower sample size, this could eliminate the memory problem.
I would instead avoid reinventing the wheel and use a library such as Picasso for loading images.
Upvotes: 1
Reputation: 1557
Have you added the relevant permission in your manifest file? If not, add this and try:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If it still crashes, please paste your logs also from logcat.. This'll help dig deeper into the real problem..
Upvotes: 0
Reputation: 61
Create Drawable Object from file path
Ex: Drawable d=Drawable.createFromPath(selectedImagePath);
uploadImage.setImageDrawable(d);
Upvotes: 1