0

little problem, I would like to display an image from an url, without using (glide, picasso etc ...), I want to learn from the errors I make, here is my code, the activity stops , do you see something that I missed?

onCreate :

  String imgUrl = "https://st3.depositphotos.com/1008939/12603/i/950/depositphotos_126032722-stock-photo-roaring-singing-woman.jpg";
    ImageView img = findViewById(R.id.imageArtist);

    try {
        InputStream input = new java.net.URL(imgUrl).openStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        img.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

xml :

<ImageView
    android:layout_width="@dimen/dim_170"
    android:layout_height="@dimen/dim_170"
    android:id="@+id/imageArtist"
    android:layout_below="@+id/txtViewTitleArtist"
    android:layout_centerInParent="true"
    android:layout_margin="@dimen/dim_15"
    tools:ignore="ContentDescription" />

thx for your help

1 Answers1

1

Here you are trying network operation in Application's main thread (in onCreate) . Before do that you should permit StrictMode ThreadPolicy.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String imgUrl = "https://st3.depositphotos.com/1008939/12603/i/950/depositphotos_126032722-stock-photo-roaring-singing-woman.jpg";
    ImageView img = findViewById(R.id.imageArtist);

    try {
        InputStream input = new java.net.URL(imgUrl).openStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        img.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

Plesae check this

Zahid Islam
  • 740
  • 1
  • 5
  • 11
  • `Before do that you should permit StrictMode ThreadPolicy.` No No. Dont learn OP these dirty tricks. OP should use a thread to execute internet code. And you too! – blackapps Aug 28 '21 at 08:07
  • He wants to learn from problem he mentioned . And it's for debugging or learning purpose only. – Zahid Islam Aug 28 '21 at 08:26