I would like to know how to crop an image from the gallery or photo taken by the camera, save and load a imageView
, as I can do this for the intent
gallery option, but for the camera option it returns me a data = null
below follows my code.
Activity EditContactivity Activity:
public class EditarContaActivity extends Activity implements OnClickListener {
private static int RESULTADO_IMAGEM_CARREGADA = 1;
private static final String filename = "profile.jpg";
private static final File file = new File(Environment.getExternalStorageDirectory(), filename);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editarconta);
imguser = (ImageView)findViewById(R.id.imgdefault_user);
btneditar = (Button)findViewById(R.id.btneditar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_editarconta, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Detecta códigos de pedido
if(requestCode == RESULTADO_IMAGEM_CARREGADA && resultCode == Activity.RESULT_OK) {
Log.i("AQUI!", "Entrou no IF");
Bitmap bitmap = null;
if(data == null) {
Log.i("AQUI!", "Câmera");
} else {
Log.i("AQUI!", "Imagem cortada");
Bundle extras = data.getExtras();
bitmap = extras.getParcelable("data");
Log.i("AQUI!", "Bitmap recebeu a imagem");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int largura = imguser.getWidth();
int altura = largura;
RelativeLayout.LayoutParams margens = (RelativeLayout.LayoutParams) imguser.getLayoutParams();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(largura, altura);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, margens.topMargin, 0, 0);
imguser.setLayoutParams(params);
imguser.setImageBitmap(bitmap);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.btneditar) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Podemos ler e escrever os meios de comunicação
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.i("PERMISSÂO", "Podemos ler e escrever os meios de comunicação");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Só podemos ler a mídia
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.i("PERMISSÂO", "Só podemos ler a mídia");
} else {
// Não podemos ler nem escrever
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.i("PERMISSÂO", "Não podemos ler nem escrever");
}
Intent carregaImagem = CarregarImagem.pegaIntencao(this, file);
startActivityForResult(carregaImagem, RESULTADO_IMAGEM_CARREGADA);
}
}
}
Upload class:
public class CarregarImagem {
public static Intent pegaIntencao(Context context, File file) {
// Cria um array de Intenções de Câmera.
final List<Intent> cameraIntencoes = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = context.getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam){
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
cameraIntencoes.add(intent);
}
// Cria Intenção de Galeria.
final Intent galeriaIntencao = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galeriaIntencao.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
executarCorte(galeriaIntencao, 1, 1, 300, 300);
// Seletor de opções do sistema de arquivos.
final Intent chooserIntent = Intent.createChooser(galeriaIntencao, "Foto de Perfil");
// Add a opção de câmera.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntencoes.toArray(new Parcelable[]{}));
return chooserIntent;
}
public static void executarCorte(Intent cortarIntencao, int aspectX, int aspectY, int outputX, int outputY) {
cortarIntencao.putExtra("crop", "true");
cortarIntencao.putExtra("aspectX", aspectX);
cortarIntencao.putExtra("aspectY", aspectY);
cortarIntencao.putExtra("scale", true);
cortarIntencao.putExtra("outputX", outputX);
cortarIntencao.putExtra("outputY", outputY);
cortarIntencao.putExtra("return-data", true);
}
}
I have already researched everything, but I have not found anything but I left a handle on the onActivityResult if case = null, but I do not know what to put there.