下载的图像未显示在Android Studio的ImageView中。

huangapple 未分类评论44阅读模式
英文:

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 = &quot;https://en.wikipedia.org/wiki/Bart_Simpson#/media/File:Bart_Simpson_200px.png&quot;;
    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(&quot;Download Image Tutorial&quot;);
            mProgressDialog.setMessage(&quot;Loading...&quot;);
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }
    }



}

Manifest file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.phil.imagesdemo&quot;&gt;

    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; &gt;&lt;/uses-permission&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

答案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

huangapple
  • 本文由 发表于 2020年4月6日 00:29:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61045775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定