gpt4 book ai didi

android - 为什么应用程序在调试期间可以工作,但在运行时却不能工作?

转载 作者:太空狗 更新时间:2023-10-29 16:24:09 26 4
gpt4 key购买 nike

我在 Android 中有一个客户端,在 C# 中有一个服务器,它们通过套接字进行通信。我有这个问题 - 如果我在 Debug模式下运行我的客户端应用程序并在正确的位置放置一个断点 - 它工作得很好,但没有它就不会。客户端向服务器发送图像地址,服务器对其进行缩略图,将其转换为 byte[] 并发回。客户端获取 byte[],将其转换回图像并显示。我还发现,当它没有获得正确的 byte[] 时,无论发送的数组的原始大小是多少,它的大小都是 2896,有时是 1448。

这是客户端:

private void connectSocket(String a){ 

try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
Socket socket = new Socket(serverAddr, 4444);
String message = a;
flag = 0;
if(a.indexOf(".jpg")>0){
flag = 1;
}

ListItems.clear();
if(!a.equalsIgnoreCase("get_drives"))){
//.....
}
if(!ListItems.isEmpty()){
if(ListItems.get(0).matches("^[A-Z]{1}:$")){
ListItems.set(0, ListItems.get(0)+"\\");
}
}
PrintWriter out = null;
BufferedReader in = null;
InputStream is = null;
try {
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
is = socket.getInputStream();
out.println(message);

String text = "";
String finalText = "";
if(flag!=1){
while ((text = in.readLine()) != null) {
finalText += URLDecoder.decode(text, "UTF-8");
}
String[] result = finalText.split("#");
for(int i = 0; i<result.length; i++)
ListItems.add(result[i]);
}
if(flag ==1){

byte[] buffer = new byte[9999];
//placing breakpoint at the next line or before it makes it work fine
int size = is.read(buffer);
//but placing a breakpoint at the line below doesn't make it work
//it starts getting another byte array
byte[] bufffer2 = new byte[size];
for(int g = 0; g<size;g++){
bufffer2[g] = buffer[g];
}
//is.read(bufffer2);
//int read = is.read(buffer);

image = (ImageView)findViewById(R.id.imageView1);
//while ((text = in.readLine()) != null) {
// byte[] b = in.readLine().getBytes();
Bitmap bmp=BitmapFactory.decodeByteArray(bufffer2,0,bufffer2.length);
image.setImageBitmap(bmp);
//}
}
adapter.notifyDataSetChanged();

} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}

} catch (UnknownHostException e) {
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
} catch (IOException e) {
Log.e("TCP", "C: IOException", e);
e.printStackTrace();
}
}

这是服务器:

public class serv {
public static void Main() {
try {
IPAddress ipAd = IPAddress.Parse("192.168.1.2");
TcpListener myList=new TcpListener(ipAd,4444);

m:
myList.Start();

Socket s=myList.AcceptSocket();

byte[] b=new byte[100];
int k=s.Receive(b);

char cc = ' ';
string test = null;
Console.WriteLine("Recieved...");
for (int i = 0; i < k-1; i++)
{
Console.Write(Convert.ToChar(b[i]));
cc = Convert.ToChar(b[i]);
test += cc.ToString();
}

string[] response = null;
ASCIIEncoding asen = new ASCIIEncoding();
switch (test)
{
default:
MyExplorer(test, s);
break;

}

s.Close();
myList.Stop();
goto m;

}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}

public static void MyExplorer(string r, Socket s){
Image imgThumb = null;
if (r.Contains(".jpg"))
{
Image image = null;
image = Image.FromFile(r);
// Check if image exists
if (image != null)
{
imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
s.Send(imageToByteArray(imgThumb));
byte[] b = imageToByteArray(imgThumb);
}
return;
}

}

public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}

}

最佳答案

InputStream 中的 read(byte []) 方法读取当前可用的字节数。这并不一定意味着以后不会有更多字节可用。所以你需要做类似的事情

while(true) {
int s = is.read(buffer);
if(s == -1) break;

// else
add read buffer to image array
}

extract image from image array

其中“图像数组”是一些字节数组,您可以在其中不断添加读取缓冲区,直到到达流的结尾。

您的代码使用断点的原因是,当您通过调试器时,整个流可用,而在非调试中,当您进行读取时,整个流尚不可用。

关于android - 为什么应用程序在调试期间可以工作,但在运行时却不能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com