英文:
Downloaded Image not displaying in ImageView Android Studios
问题
package com.phil.imagesdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
    String url = "https://en.wikipedia.org/wiki/Bart_Simpson#/media/File:Bart_Simpson_200px.png";
    ImageView image;
    Button button;
    ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.theImage);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DownloadImage().execute(url);
            }
        });
    }
    private class DownloadImage extends AsyncTask {
        @Override
        protected Bitmap doInBackground(Object... urls) {
            String imageURL = (String)urls[0];
            Bitmap bitmap = null;
            try {
                // Download Image from URL
                InputStream input = new java.net.URL(imageURL).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            // Set the bitmap into ImageView
            image.setImageBitmap ((Bitmap)result);
            // Close progressdialog
            mProgressDialog.dismiss();
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Download Image Tutorial");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }
    }
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phil.imagesdemo">
    <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
英文:
I am having little problem with android studios. I am trying to download an image from a URL and then display it in an Imageview using AsyncTask. But when I click the button all you see is the progress dialog and then the imageview goes blank.
I did insert the uses INTERNET persmission in the manifest file.
Thanks in advance
package com.phil.imagesdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
    String url = "https://en.wikipedia.org/wiki/Bart_Simpson#/media/File:Bart_Simpson_200px.png";
    ImageView image;
    Button button;
    ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.theImage);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DownloadImage().execute(url);
            }
        });
    }
    private class DownloadImage extends AsyncTask{
        @Override
        protected Bitmap doInBackground(Object... urls) {
            String imageURL = (String)urls[0];
            Bitmap bitmap = null;
            try {
                // Download Image from URL
                InputStream input = new java.net.URL(imageURL).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            // Set the bitmap into ImageView
            image.setImageBitmap ((Bitmap)result);
            // Close progressdialog
            mProgressDialog.dismiss();
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Download Image Tutorial");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }
    }
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phil.imagesdemo">
    <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
答案1
得分: 0
好的,以下是翻译好的部分:
好的,在经过长时间的努力后,我发现错误消息是套接字失败:EPERM(操作不允许)。
我只需要从模拟器中卸载该应用,然后再次运行即可。
英文:
Okay so after a long struggle I found that the error message was socket failed: EPERM (Operation not permitted).
I just needed to uninstall the app from the emulator and then run in again
专注分享java语言的经验与见解,让所有开发者获益!

评论