3
I would like to know how to save a gallery image and click on Activity in version 2.3.3 of the Eclipse emulator, because my code only works when I test on my Android device 4.1 and even then when I upload a large image it leaves a huge white space on 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);
}
}
While running in 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 I used in my code I found that the error is on this line:
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
The this.getContentResolver()
is returning null
, but this error only occurs in Android emulator 2.3.3, already on my device(Android 4.1) it runs normal.
Are you sure it’s
getContentResolver()
that returns null? It won’t beselectedImage
which is null?.– ramaral
@ramaral Pois é fiz um teste aqui e não é o getContentResolver() que retorna null, mas fui tentar um teste para ver o selectedImage retorna null não tive secusso, pus um if(selectedImage == null) e não executa o que está no if(). To the.
– Gustavo Almeida Cavalcante
@ramaral The problem I realize is that it cannot create and save a file even with the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> on Androidmanifest
– Gustavo Almeida Cavalcante