android - 如何通过套接字 Android Socket 发送图像位图
问题描述
我的老师给了我一个作业,一个在 android studio 中的小项目。
我想取得好成绩,所以我尝试创建一个允许
在连接到同一 WIFI 网络的两部手机之间传输照片的应用程序。(带套接字)
我创建了一个应用程序,可让您向服务器发送字符串,但我不知道如何发送照片。(照片选择部分完成)
有我的客户代码:
void client() throws IOException{
ipAddr = ipAddrEditText.getText().toString();
InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap imageBitMap = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
imageBitMap.compress(Bitmap.CompressFormat.PNG,0, bos);
byte[] array = bos.toByteArray();
Socket s = new Socket(ipAddr,7800);
OutputStream out = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(array.length);
dos.write(array,0,array.length);
dos.flush();
dos.close();
out.close();
s.close();
}
有服务器的代码:
void server() {
ServerSocket ss = null;
try {
ss = new ServerSocket(7800);
DataInputStream dis;
int len;
byte[] data;
Socket s;
while(true) {
s = ss.accept();
InputStream is = s.getInputStream();
dis = new DataInputStream(is);
len = dis.readInt();
data = new byte[len];
dis.readFully(data,0,data.length);
photoBitmap = BitmapFactory.decodeByteArray(data,0,data.length);
img.setImageBitmap(photoBitmap);
dis.close();
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
每次我尝试传输照片时,服务器都会崩溃。
------------------------------------------------
日志猫错误:
Process: com.example.theminiproject, PID: 15123
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:10554)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:2113)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3239)
at android.view.View.requestLayout(View.java:27069)
at android.widget.ImageView.setImageDrawable(ImageView.java:600)
at androidx.appcompat.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:104)
at android.widget.ImageView.setImageBitmap(ImageView.java:764)
at androidx.appcompat.widget.AppCompatImageView.setImageBitmap(AppCompatImageView.java:112)
at com.example.theminiproject.MainActivity2.server(MainActivity2.java:70)
at com.example.theminiproject.-$$Lambda$jVrGEiH1Ep-S9RdZnfrW_UVFTWs.run(Unknown Source:2)
at java.lang.Thread.run(Thread.java:923)
2021-03-08 12:24:31.431 15123-15123/? I/ViewRootImpl@867b75a[MainActivity]: stopped(false) old=true
2021-03-08 12:24:31.434 15123-15123/? I/ViewRootImpl@867b75a[MainActivity]: stopped(false) old=false
2021-03-08 12:24:31.441 15123-26157/? I/Process: Sending signal. PID: 15123 SIG: 9
因为我是初学者,所以我什么都不懂。
谢谢你。
解决方案
感谢评论部分的帮助,我在代码中发现了错误。
一个 Thread 不能改变一个 ImageView,你必须创建一个单独的函数来改变它。
在我的情况下,我创建了一个更改图像的按钮。
public void changePhoto(View v) {
img.setImageBitmap(photoBitmap);
}
- 如果你通过 Thread 调用函数,照片会改变,但应用程序会在 0.1 秒后崩溃。
感谢所有帮助过的人。
推荐阅读
- javascript - 为什么当我将 div 标签 innerHTML 属性分配给变量时它没有改变?
- java - 更改 NatTable 颜色
- arrays - 没有结构的链表,但只使用数组
- c# - 如何在 C# 中的 XMLNode 上插入新元素
- docker - Docker容器A无主机卷访问Docker容器B的文件系统
- c# - 将 json 有效负载转换为字符串
- python - 如何编写一行代码,用于在一行中用 27 个字符翻转硬币
- python - python中pandas数据的指数曲线拟合
- ios - startMonitoringSignificantLocationChanges 未立即启动
- tuples - 为什么我的语法是错误的?(TypeError:“元组”对象不可调用)