需求的代码如下:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText etAddress;
    private Button btnGetImage;
    private ImageView ivImage;

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

        etAddress = findViewById(R.id.et_address);
        btnGetImage = findViewById(R.id.btn_get_image);
        ivImage = findViewById(R.id.iv_image);

        btnGetImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String address = etAddress.getText().toString().trim();
                if (!TextUtils.isEmpty(address)) {
                    new DownloadImageTask().execute(address);
                }
            }
        });
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... params) {
            String address = params[0];
            Bitmap bitmap = null;
            try {
                URL url = new URL(address);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                bitmap = BitmapFactory.decodeStream(input);
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                ivImage.setImageBitmap(bitmap);
                saveAddressToFile(etAddress.getText().toString().trim());
            }
        }
    }

    private void saveAddressToFile(String address) {
        try {
            FileOutputStream fos = openFileOutput("address.txt", MODE_PRIVATE);
            fos.write(address.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

activity_main.xml

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

    <EditText
        android:id="@+id/et_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入网络图片地址" />

    <Button
        android:id="@+id/btn_get_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取并显示图片" />

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background" />

</LinearLayout>
``
上面是一 个EditText控件用来输入网络图片地址中间是一个按钮获取并显示图片最下面是个ImageView控件用来显示图片。将地址存储到一个内部存储文件中文件名是addresstxt。用Android实现以上

原文地址: https://www.cveoy.top/t/topic/huJW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录