java - 接收图像并使用 TCP 套接字显示它? (安卓)

标签 java android sockets mobile tcp

我如何接收图像并通过 Android 设备的 tcp 套接字显示它?我尝试了一些东西,但没有用。我正在寻找一些从电脑发送图像或文件并将其保存在手机上的示例(android,java)

最佳答案

我使用类似的方法从网址下载图像:
导入java.io.IOException; 导入 java.io.InputStream; 导入 java.net.HttpURLConnection; 导入java.net.URL; 导入 java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class AsyncDownloadImage extends AsyncTask<ImageView, Void, Bitmap> {

    private static final String TAG = "AsyncDownloadImage";
    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return DownloadImage((String) imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null)
            imageView.setImageBitmap(result);
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            if (in != null)
                in.close();
        } catch (IOException e1) {
            Log.e(TAG, "Error in downloading image");
            e1.printStackTrace();
        }
        return bitmap;
    }
}

我使用它的方式是在imageview的标签中设置我想要下载的图像的url并将其作为参数传递给ImageView。例如。

ImageView iv.setTag("http://www.example.com/image.png");
new AsyncDownloadImage().execute(iv);

如果您想使用套接字下载它,您可以打开套接字连接,例如:

Socket socket = new Socket(ip, port);
InputStream inputStream = socket.getInputStream();

关于java - 接收图像并使用 TCP 套接字显示它? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811236/

相关文章:

android - 在哪里可以找到 .apk 文件?

android - 适用于 android、iphone 和 windows phone 的通用编程语言

android - 重复问题(AlarmManager)

java - 使用来自 C++ 的 java 套接字接收 float

java - 使用 Spring Batch 从控制台中断时如何将作业标记为失败?

java - 将 SortedSet 转换为 TreeMap 或 ImmutableMap,并将键作为迭代器位置

java - 从运行时进程 java 捕获错误

java - 检索哈希数,git编译war

java - 通过可序列化的套接字传输对象

java - 带套接字的 BufferedReader 和 BufferedWriter