Ok, so I have looked through various answers on this site and in practice they all seem great and pretty much the standard way of doing this. Well, standard has failed me. So, I want to have an ImageButton access the gallery and have the user select an image. After the user selects that image I want it to become the ImageButton's background. My code so far is:
package ion.takedown;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class newWrestler extends Activity {
private String selectedImagePath;
private ImageButton wrestlerPicture;
@Override
protected void onCreate(Bundle newWrestler) {
super.onCreate(newWrestler);
setContentView(R.layout.new_wrestler);
wrestlerPicture = (ImageButton) findViewById(R.id.wrestlerPhoto);
wrestlerPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
wrestlerPicture.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please help me It is really getting on my nerves now haha...