I would like to know how to save an image from the gallery and load the activity in version 2.3.3 of the Eclipse emulator, because my code only works when I test it on my Android 4.1 device and even then when I load a large image it leaves a huge white space at the top and bottom of the image.
My code goes below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("AQUI!", "Entrou no onActivityResult");
//Detects request codes
if(requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK) {
Log.i("AQUI!", "Entrou no IF");
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
Log.i("AQUI!", "Bitmap recebeu a imagem");
imguser.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.btneditar) {
String filename = "profile.jpg";
// cria o arquivo
File file = new File(Environment.getExternalStorageDirectory(), filename);
Intent cropIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
cropIntent.putExtra("return-data", false);
startActivityForResult(cropIntent, RESULT_LOAD_IMAGE);
}
}
When I run the emulator I get the following Log error:
01-23 18:31:36.146: E/AndroidRuntime(336): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=file:///mnt/sdcard/profile.jpg (has extras) }} to activity {com.example.meuapp/com.example.meuapp.atividades.EditarContaActivity}: java.lang.NullPointerException
Thanks to the Log.i's that I used in my code I discovered that the error is in this line:
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
this.getContentResolver()
is returning null
, but this error only occurs in the Android 2.3.3 emulator, it already runs on my device (Android 4.1).