1. 前言
最近在完成学校的仿QQ实现的Android项目,写到了修改头像的部分,需要在前端提交一个头像文件,并且在后端存储到项目的resource/img下面,同时又能在前端中访问到这个图片下载下来节省每次访问的资源消耗,就想写以下如何访问到后端Spring项目中的图片资源。
2. layout布局
写的一个简单的布局文件,只有一个显示图片的ImgView和一个刷新图片的按钮

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center">
<ImageView android:id="@+id/img" android:layout_width="300dp" android:layout_height="300dp" android:scaleType="fitXY" />
</LinearLayout>
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="100dp" android:id="@+id/resit" android:text="重置" android:textSize="30dp"/> </LinearLayout>
|
3. Spring部分
由于只需要读取图片部分,所以简单配置了一个静态资源文件的路径
1 2 3 4
| spring: web: resources: static-locations: classpath:/static/
|
4. MainActivity
这里是主要的实现加载图片的方,其中也写了注释,通俗易懂,不过在这里没有使用到之前的自定义的HTTPUtil方法,使用了一个OKHttp的外部资源包,可以简化手写各种访问的工具包:
1
| implementation("com.squareup.okhttp3:okhttp:4.9.0")
|

主要实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.example.dowimg;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;
import java.io.IOException; import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private ImageView imgView; private Button resetButton;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
imgView = findViewById(R.id.img); resetButton = findViewById(R.id.resit);
resetButton.setOnClickListener(v -> loadImageFromServer("2237023838"));
loadImageFromServer("2237023838"); }
private void loadImageFromServer(String imgName) { new ImageDownloadTask().execute("https://<花生壳地址>/img/" + imgName+".jpg"); }
private class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
@Override protected Bitmap doInBackground(String... urls) { String url = urls[0]; Bitmap image = null; OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder() .url(url) .build();
try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { InputStream inputStream = response.body().byteStream(); image = BitmapFactory.decodeStream(inputStream); } } catch (IOException e) { e.printStackTrace(); } return image; }
@Override protected void onPostExecute(Bitmap bitmap) { if (bitmap != null) { imgView.setImageBitmap(bitmap); } else { Toast.makeText(MainActivity.this, "图片加载失败", Toast.LENGTH_SHORT).show(); } } } }
|
5. 联网权限
项目需要使用到联网的权限,所以需要在AndroidManifest.xml中声明:
1
| <uses-permission android:name="android.permission.INTERNET"/>
|
6. 运行结果
图片正常存在spring后端:

手机也能正常访问:
