I am trying to set a image to a ImageView in android, by picking it from the gallery. I'm using intent to open the gallery, and OnActivityResult, to handle it. The problem is that the OnActivityResult is not working. The gallery opens, I choose the image, and then it is still the old image. There are no errors.
Here is my code for opening the gallery:
wallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
And thats how I'm handling it:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
wallpaper.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();
// Even the Toast is not working.
}
}
I am sure, that the ImageView is declared correctly, because when I use resource as a wallpaper it is working. Any idea where is the problem coming from?
Edit: onActivityResult() IS being called, but the code inside the IF is not being accessed / triggered.
This is the ImageView, which I need to change:
<HorizontalScrollView
android:id="@+id/wallScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageView
android:id="@+id/wallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wall"
android:scaleType="centerCrop"/>
</HorizontalScrollView>
I found out that this is somehow causing the problems any idea why?
Edit: ImageView declaration:
before OnCreate():
ImageView wallpaper;
inside onCreate():
wallpaper = (ImageView)findViewById(R.id.wallpaper);
and the onClickListener is already above.
I just find out that the onActivityResult() is immediately called after the gallery is open. Its not triggering the IF, because it doesn't wait me to pick a image. Any idea why is that happening?
Solved:
The problem was coming from that, that when the gallery was opened, my activity was restarted, and because of that OnActivityResult() was called too early. This doesn't happen on all devises but to be sure that it is going to work on all devices just add to the manifest:
android:launchMode="singleTop"
Everybody, thanks for your help!
This is an running example Activity class...
package com.example.testproject;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
ImageView ivSelectedPhoto;
public static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivSelectedPhoto = (ImageView) findViewById(R.id.ivGalery) ;
Button btnOpenGalery = (Button) findViewById(R.id.btnSelectImage);
btnOpenGalery.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent Data) {
super.onActivityResult(RequestCode, ResultCode, Data);
if (RequestCode == RESULT_LOAD_IMAGE && ResultCode == RESULT_OK && null != Data) {
Uri SelectedImage = Data.getData();
String[] FilePathColumn = {MediaStore.Images.Media.DATA };
Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
SelectedCursor.moveToFirst();
int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
String picturePath = SelectedCursor.getString(columnIndex);
SelectedCursor.close();
ivSelectedPhoto.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();
}
}
}
I am making now for you :) You don't forget to add @Override on onActivityResult() method :)