Make it work just the same, but you have to know where the image comes from. Remembering that permissions must be added:
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
...
private final int ALTERAR_IMAGEM_INTENT = 1;
private Uri fotoCameraUri; //vai gravar o caminho da foto
...
This method will add both cases, photos from the file, or camera photo.
private void alterarImagem() {
File fotoCamera = new File(Environment.getExternalStorageDirectory()
+ "/DCIM/", "image" + System.currentTimeMillis() + ".png");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Do something for lollipop and above versions
this.fotoCameraUri = FileProvider.getUriForFile(getApplicationContext(), this.getApplicationContext().getPackageName() + ".provider", fotoCamera);
} else {
// do something for phones running an SDK before lollipop
this.fotoCameraUri = Uri.fromFile(fotoCamera);
}
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final List<ResolveInfo> listCam = getPackageManager().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, this.fotoCameraUri);
cameraIntents.add(intent);
}
// Filesystem. Irá verificar
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.cadastro_concluir_acao_utilizando));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, ALTERAR_IMAGEM_INTENT);
}
No activity on result should check where the image came from and set it where it should be added in your imageview, for example:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
final boolean isCamera = ImageUtils.isCamera(data);
if (!isCamera && (data.getData() == null || this.getContentResolver().getType(data.getData()) == null
|| !this.getContentResolver().getType(data.getData()).startsWith("image"))) {
Toast.makeText(this, getString(R.string.cadastro_string_arquivo_selecionando_nao_e_imagem), Toast.LENGTH_LONG).show();
return;
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = this.fotoCameraUri;
} else {
selectedImageUri = data.getData();
}
switch (requestCode) {
case ALTERAR_IMAGEM_INTENT:
bitmapImagemUsuario = //faça o decode da sua imagem
if (bitmapImagemUsuario != null) {
//faça o que tiver de fazer
circleImageViewUsuario.setImageBitmap(bitmapImagemUsuario);
}
break;
}
}
}
It can also be, just the lack of permission, as I do not see your code. I put one here to help more people too.