Android - 如何将文件从服务器套接字保存到SD卡pdf和jpg ...?

时间:2011-09-23 07:19:40

标签: android sockets

我在我的PC和客户端上安装了服务器...我通过socket和客户端android读取并显示成功显示jpg文件。但我想把它保存到SD卡。请帮忙......

1 个答案:

答案 0 :(得分:3)

private void copytoSD() throws IOException{

            //Open your local file as the input stream
            InputStream myInput = //your input Stream

            // Path to the just created empty db
            String outFileName = Environment.getExternalStorageDirectory().toString+"/filename.jpg";

            //Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[2048];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            //Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }