4

我从客户端套接字接收 jpeg 图像(图像大小:50KB)并保存在模拟器 SD 卡中。从那里我在 Imageview 中显示 jpg 图像。但是我想在将图像保存到 SD 卡之前显示图像,因为我们的 android 应用程序会从套接字接收连续的图像,如果我遵循接收、保存和显示方法,那么它会变得非常慢,所以要提高我想要的速度仅从 ram 显示。为此,我需要将图像阵列临时保存在 RAM 上。从那里我计划通过使用单独的线程来显示和保存。所以请指导我如何从字节数组中显示图像。

注意:我从套接字接收 JPEG 图像,而不是 .bmp 或 .gif 或 .png。

下面是我从 tcp 套接字接收图像的代码。(它工作正常)(注意:这是在单独的线程中完成的,不要在 UI 线程中尝试。)

                    public byte[] mybytearray  = new byte[310000];
                    private int bytesRead=0;
                    private int current = 0;

                    ServerSocket serverSocket = new ServerSocket(SERVERPORT);  
                    Socket client = serverSocket.accept(); 


                   try {

                       myDir=new File("/mnt/sdcard/saved_images");

                        if (!myDir.exists()){
                            myDir.mkdir();
                        }else{
                            Log.d("ServerActivity","Folder Already created" );
                        }

                        String fpath = "/image0001.jpg";
                        File file = new File (myDir, fpath);
                        if (file.exists ()) file.delete ();


                        InputStream is = client.getInputStream();
                        FileOutputStream fos = new FileOutputStream(file);
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        bytesRead = is.read(mybytearray,0,mybytearray.length);
                        current = bytesRead;

                 do {
                      bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
                      if(bytesRead >= 0) current += bytesRead;

                 } while(bytesRead > -1);

                        bos.write(mybytearray, 0 , current);

                        Log.d("ServerActivity","Reconstructing Image from array");

                        bos.flush();
                        bos.close();
                        fos.flush();
                        fos.close();
                        is.close();
                        client.close();
                        serverSocket.close();
                    } catch (Exception e) { 
                  e.printStackTrace();
              }
4

3 回答 3

10

尝试将此代码段插入到您的代码中:

do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

ByteArrayInputStream inputStream = new ByteArrayInputStream(myByteArray);
bitmap = BitmapFactory.decodeStream(inputStream);
ImageView picture = new ImageView(this);
picture.setImageBitmap(bitmap);

bos.write(mybytearray, 0 , current);
于 2013-01-10T12:14:27.600 回答
5

使用位图,从字节数组创建它,

Bitmap bitmap;
bitmap= BitmapFactory.decodeByteArray(mybytearray, 0, mybytearray.length);
于 2013-01-10T12:13:39.423 回答
-1

将 byte[] 转换为位图。尝试以下

ByteArrayInputStream imageStream = new ByteArrayInputStream(byte[] array);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
于 2013-01-10T12:13:03.427 回答