Imagem não é carregada depois do CROP.

  • Respostas:0
Maherbson
  • Posts no fórum: 1

05/04/2016, 16:00:16 via Web

Bom dia senhores e senhoras, me desculpem se o código não nos padrões do fórum pois sou novo aqui, mas minha dúvida(Problema) é o seguinte, na tela de cadastro selecione uma imagem da galeria e envio para a Activity de CROP onde consigo obter a imagem normalmente, quando clico em OK para recortar a imagem e enviar para a Activity de cadastro, envio uma string base64, consigo ver pelo log que ela chega completa, mas quando converto para um array de bytes e em seguida para bitmap não consigo fazer com que a imagem apareça no ImageView. É como se ao converter a imagem se perdesse. Grato!

    public class Cadastrar extends AppCompatActivity {

private static ImageView telaImagem;
private static Bitmap bitmap;

@TargetApi(Build.VERSION_CODES.KITKAT)
    protected void onActivityResult(int requestCode, int resultCode, final Intent imageReturnedIntent) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_FILE) {
                uri = imageReturnedIntent.getData();
                Log.i("INTENT", String.valueOf(uri));
                try {
                    //bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    //ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    //bitmap.compress(Bitmap.CompressFormat.JPEG, 30, byteArrayOutputStream);

                    InputStream inputStream = getContentResolver().openInputStream(uri);

                    //CRIANDO UM SHAREDPREFERENCES PARA PASSAR A URI DA IMAGEM SELECIONADA DA GALERIA PARA A TELA DE CROP
                    SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("foto", String.valueOf(uri));
                    editor.apply();

                    Intent intent = new Intent(Cadastrar.this, Crop.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (requestCode == REQUEST_CAMERA) {
                onCaptureImageResult(imageReturnedIntent);
            }
        }
    }


public static void receberImagemRecortada(String stringImagem) {
        byte[] encode = Base64.decode(stringImagem, Base64.DEFAULT);
        bitmap = BitmapFactory.decodeByteArray(encode, 0, encode.length);
        telaImagem.setImageBitmap(bitmap);

private Bitmap decodeBitMap(InputStream inputStream) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferQualityOverSpeed = false;
        options.inSampleSize = 4;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        return BitmapFactory.decodeStream(inputStream, null, options);
    }

}

public class Crop extends AppCompatActivity {

private CropImageView mCropImageView;
private ImageView ivImage;

List<ReceberImagem> enviarImagem;

private Button btOk;

public static final String PREFS_NAME = "Preferences";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.teste);

   //RECEBO A URI DA ACTIVITY CADASTRO
    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
    String id = prefs.getString("foto", "");

    mCropImageView = (CropImageView) findViewById(R.id.mCropImageView);

    InputStream inputStream = null;
    try {
        inputStream = getContentResolver().openInputStream(Uri.parse(id));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    mCropImageView.setImageBitmap(decodeBitMap(inputStream));
}

// LISTENERS
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void rotate(View view) {
    if (view.getId() == R.id.bt0) {
        mCropImageView.rotateImage(90);
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void statusCropImage(View view) {
    Matrix matrix = new Matrix();
    matrix.setRotate(mCropImageView.getRotation());
    Bitmap auxBitmap = Bitmap.createScaledBitmap(mCropImageView.getCroppedImage(), mCropImageView.getCroppedImage().getWidth(),
            mCropImageView.getCroppedImage().getHeight(), true);
    auxBitmap = Bitmap.createBitmap(auxBitmap, 0, 0, auxBitmap.getWidth(), auxBitmap.getHeight(), matrix, true);

    //PASSANDO STRING DE BASE64 PARA A ACTIVITY CADASTRAR
    CadastrarPet.receberImagemRecortada(getStringImagem(auxBitmap));

    Intent intent = new Intent(Crop.this, CadastrarPet.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

    //mCropImageView.setImageBitmap(auxBitmap);
}

public String getStringImagem(Bitmap bitm) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imagemBytes = stream.toByteArray();
    String encodeImage = Base64.encodeToString(imagemBytes, Base64.DEFAULT);
    return encodeImage;
}

private Bitmap decodeBitMap(InputStream inputStream) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferQualityOverSpeed = false;
    options.inSampleSize = 4;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = false;
    return BitmapFactory.decodeStream(inputStream, null, options);
}

}

Responder