Why Can't I Show an Image From Url in Android Studio?

2.9k Views Asked by At

For some reason, I can't show image from URL in android studio. Whatever I tried did not happen. First I tried "Picasso", then I tried "Universal Image Loader", then I tried "Firebase Storage" and "Glide". But I couldn't show an image on the internet in any of them. Application works with these 4 methods (Picasso,Universal Image Loader,Firebase Storage), but it does not show the image.

I will only show picasso below so that the problem is not long, but if you want I can show them all. Is there something I have overlooked? Or maybe a few simple settings that I don't know are necessary? so whatever I try, what could be preventing me from showing the image.

Gradle


    apply plugin: 'com.android.application'
android { compileSdkVersion 29 buildToolsVersion "29.0.3"

defaultConfig {
    applicationId "com.deneme.goruntudeneme"
    minSdkVersion 19
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}}
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
}

XML


    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ImageView
    android:id="@+id/my_image_view"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:scaleType="centerCrop">
</ImageView></LinearLayout>

Java


    public class MainActivity extends AppCompatActivity {

ImageView imageView;

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

    imageView=(ImageView)findViewById(R.id.my_image_view);

    Picasso.get().load("https://via.placeholder.com/500").into(imageView);

}}

Permissons


    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2

There are 2 best solutions below

1
JstBora On BEST ANSWER

As @Ashis said, my problem was solved when I added the following line in the manifest.

android:usesCleartextTraffic="true"
0
Alok Singh On

When you load a image using picaso then the end url must be end with extension like (.jpg or .png) like this "https://i.stack.imgur.com/jEIKP.jpg". Paste this url then try its working problem is "Your URL is wrong"