I'm trying to load an image from my API to show it as a profile image, the problem is that the user opens the navigator for the first time, the image does not load, and this makes the whole layout disappear, but from the second time it all works, and I notice that this happens when the width of the image is less than the height. This is the class I'm using:
public class CircledNetworkImageView extends ImageView { public boolean mCircled; private String mUrl; private int mDefaultImageId; private int mErrorImageId; private ImageLoader mImageLoader; private ImageLoader.ImageContainer mImageContainer; public CircledNetworkImageView(Context context) { this(context, null); } public CircledNetworkImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircledNetworkImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setImageUrl(String url, ImageLoader imageLoader) { mUrl = url; mImageLoader = imageLoader;
I came up with this solution, but I did not like it, because the image loses its quality:
public class UserActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile_main); toolbar = (Toolbar) findViewById(R.id.toolbar_user); camera = (ImageView) findViewById(R.id.camera); fechar = (TextView) findViewById(R.id.fechar); editar = (TextView) findViewById(R.id.editar); membro = (TextView) findViewById(R.id.membro); nome = (TextView) findViewById(R.id.nome_usuario); email = (TextView) findViewById(R.id.email_usuario); email = (TextView) findViewById(R.id.email_usuario); profilePic = (NetworkImageView) findViewById(R.id.foto); mImageView = (ImageView) findViewById(R.id.cropped); progress_wheel = (ProgressWheel) findViewById(R.id.progress_wheel); camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder getImageFrom = new AlertDialog.Builder(UserActivity.this); getImageFrom.setTitle("Abrir com:"); final CharSequence[] opsChars = {getResources().getString(R.string.takepic), getResources().getString(R.string.opengallery)}; getImageFrom.setItems(opsChars, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == 0) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); try { intent.putExtra("return-data", true); startActivityForResult(intent, PICK_FROM_CAMERA); } catch (ActivityNotFoundException e) { e.printStackTrace(); } } else if (which == 1) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE); } dialog.dismiss(); } }); getImageFrom.show(); } }); profilePic.setImageUrl(GlobalModel.getPerfil().getIdDaImagem(), imageLoader); profilePic.setDefaultImageResId(R.drawable.avatar_); } @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (resultCode != RESULT_OK) return; switch (requestCode) { case PICK_FROM_CAMERA: profilePic.setVisibility(View.GONE); progress_wheel.setVisibility(View.VISIBLE); selectedImagePath = ImageFilePath.getPath(getApplicationContext(), mImageCaptureUri); Log.i("Image File Path", "" + selectedImagePath); mImageCaptureUri = Uri.parse(selectedImagePath); final BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 8; Bitmap photo = BitmapFactory.decodeFile(mImageCaptureUri.getPath(), option); photo = Bitmap.createScaledBitmap(photo, 200, 200, false); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File f = new File(Environment.getExternalStorageDirectory() + File.separator + "Imagename.jpg"); f.createNewFile(); FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); fo.close(); previewCapturedImage(f.getAbsolutePath()); profilePic.setVisibility(View.VISIBLE); progress_wheel.setVisibility(View.GONE); profilePic.setImageBitmap(BitmapFactory.decodeFile(mImageCaptureUri.getPath())); break; case PICK_FROM_FILE: profilePic.setVisibility(View.GONE); progress_wheel.setVisibility(View.VISIBLE); mImageCaptureUri = data.getData(); selectedImagePath = ImageFilePath.getPath(getApplicationContext(), mImageCaptureUri); Log.i("Image File Path", "" + selectedImagePath); mImageCaptureUri = Uri.parse(selectedImagePath); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap photoGaleria = BitmapFactory.decodeFile(mImageCaptureUri.getPath(), options); photoGaleria = Bitmap.createScaledBitmap(photoGaleria, 200, 200, false); ByteArrayOutputStream bytesG = new ByteArrayOutputStream(); photoGaleria.compress(Bitmap.CompressFormat.JPEG, 100, bytesG); File fG = new File(Environment.getExternalStorageDirectory() + File.separator + "Imagename.jpg"); fG.createNewFile(); FileOutputStream foG = new FileOutputStream(fG); foG.write(bytesG.toByteArray()); foG.close(); previewCapturedImage(fG.getAbsolutePath()); profilePic.setVisibility(View.VISIBLE); progress_wheel.setVisibility(View.GONE); profilePic.setImageBitmap(BitmapFactory.decodeFile(mImageCaptureUri.getPath())); break; } } catch (Exception e) { e.printStackTrace(); } } private void previewCapturedImage(String path) { try { UploadFoto mUpload = new UploadFoto(path); mUpload.setEventoListener(new IExecutarTarefa<UploadFoto>() { @Override public void AntesDeExecutar(UploadFoto tarefa) { } @Override public void DepoisDeExecutar(UploadFoto tarefa) { if (tarefa.getResposta()[0].equals("200")) { GlobalModel.getPerfil().setIdDaImagem(WebService.imgURL + tarefa.getResposta()[1].replace("\"", "")); profilePic.setImageUrl(GlobalModel.getPerfil().getIdDaImagem(), imageLoader); } } }); mUpload.execute(); } catch (NullPointerException e) { e.printStackTrace(); } } @Override public void onBackPressed() { super.onBackPressed(); finish(); overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave); } @Override protected void onResume() { super.onResume(); CarregarPerfil mCarrega = new CarregarPerfil(); mCarrega.execute(); } }
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:wheel="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <include android:id="@+id/toolbar_user" layout="@layout/toolbaruser" /> <RelativeLayout android:id="@+id/campoImagem" android:layout_width="fill_parent" android:layout_height="200dp" android:layout_below="@+id/toolbar_user" android:background="@color/armadillo"> <com.android.volley.toolbox.NetworkImageView android:id="@+id/foto" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/user" android:visibility="visible" /> <com.pnikosis.materialishprogress.ProgressWheel android:id="@+id/progress_wheel" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center" android:visibility="gone" wheel:matProg_barColor="@color/white" wheel:matProg_progressIndeterminate="true" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageView android:id="@+id/cropped" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_gravity="center" android:visibility="visible"/> <ImageView android:id="@+id/camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:padding="15dp" android:src="@drawable/ic_camera" /> <TextView android:id="@+id/Button_crop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:gravity="center" android:padding="15dp" android:text="Salvar" android:textColor="@color/white" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <TextView android:id="@+id/membro" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:gravity="center" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:text="Membro Retornar desde Julho de 2015" android:textColor="@color/star_dust" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/campoImagem" android:layout_marginBottom="30dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:paddingBottom="0dp" android:paddingLeft="10dp" android:paddingRight="0dp" android:paddingTop="10dp" android:text="Nome" android:textColor="@color/star_dust" android:textSize="@dimen/text1" /> <TextView android:id="@+id/nome_usuario" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:paddingBottom="0dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:textColor="@color/armadillo" android:textSize="@dimen/text1" /> <TextView android:id="@+id/entrar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:paddingLeft="10dp" android:paddingTop="10dp" android:text="E-mail" android:textColor="@color/star_dust" android:textSize="@dimen/text1" /> <TextView android:id="@+id/email_usuario" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:paddingBottom="0dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:text=" jgvidotto@gmail.com " android:textColor="@color/armadillo" android:textSize="@dimen/text1" /> </LinearLayout> </RelativeLayout>