How to pass a String to an imageview

Asked

Viewed 65 times

0

I have a big doubt, I passed an image from one screen to another and captured his URL, but I need to transform it to Bitmap, and exhibited it in a Imageview.

Follows my code:

public class ComentarActivity extends AppCompatActivity {

    private EditText comentario;
    private Button botaoSalvar;
    private ImageView fotoSelecionada;
    private String obID;
    private ViewPager viewPager;
    private TextView texto;
    Context context;



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

        comentario = (EditText) findViewById(R.id.text_comentario);
        botaoSalvar = (Button) findViewById(R.id.button_salvar);
        fotoSelecionada = (ImageView) findViewById(R.id.imageView2);



        //passando uma foto da galeria para a imageviewe


        Intent i = getIntent();
        obID = i.getStringExtra("imagem");

        byte[] decodedString = Base64.decode(obID, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        Drawable foto = new BitmapDrawable(getResources(), decodedByte);
        fotoSelecionada.setImageBitmap(foto);

Any suggestions??

  • Does this URL give access to an external image (from any site)? What problem with your code?

  • Did you mean URI? With Indian i? Or is it URL itself?

1 answer

0

vc can use the Picasso library to load the image in Imageview.

import the library:

Pile 'com.squareup.Icasso:Icasso:2.5.2'

If you are in your Activity for example, so set the path in Activityresult after selecting the gallery photo :

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

     Uri selectedPictureUri = data.getData();
     String picturePath = getRealPathFromURI(selectedImageUri);
     loadIntoImageView(picturePath);
}

public void loadIntoImageView(String path){
     Picasso.with(context)
                    .load(path)
                    .into(imageView);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.