在android中记录并上传音频和视频文件到服务器

时间:2018-03-08 06:42:18

标签: android performance

在我的应用程序中,我需要以较小的尺寸录制音频或视频,并需要将其保存到媒体并发送到服务器。这样做的最佳方法是什么?不使用任何第三方库。请指导我。谢谢。

2 个答案:

答案 0 :(得分:0)

最好的方法是在服务器上创建API,然后将参数传递给该API。主要功能将由API执行。

答案 1 :(得分:0)

将文件发送到服务器,您需要一个上传URL和录音的文件名,这里是代码

代码说明

  1. 打开网址连接,获取要写入数据的信息流。

  2. 指定一个包含流信息的最大缓冲区。

  3. 3.从文件中读取字节并将其写入输出流。

    4.获得回复并相应地采取必要的行动。

    public boolean uploadFileNew(String sourceFileUri) {
            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            DataInputStream inputStream = null;
    
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
    
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            File upFile=null;       
    
            try {
                final File root = new File((Environment.getExternalStorageDirectory() + File.separator + "DIR_NAME"));
    
    
                root.mkdirs();
                final String tmp[] = sourceFileUri.split(File.separator);
                final String fname = tmp[tmp.length-1];
                upFile = new File(root, fname);
    
    
    
                    FileInputStream fileInputStream = new FileInputStream(upFile);
    
    
                    URL url = new URL("YOUR_URL");
                    connection = (HttpURLConnection) url.openConnection();
    
                    // Allow Inputs & Outputs.
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
    
                    // Set HTTP method to POST.
                    connection.setReadTimeout(10000);
                    connection.setConnectTimeout(15000);
                    connection.setRequestMethod("POST");
    
                    connection.setRequestProperty("Connection", "Keep-Alive");
    
                    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    
                    outputStream = new DataOutputStream(connection.getOutputStream());
                    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    
                    outputStream.writeBytes("Content-Disposition: form-data; typ=\""+1+"\"; name=\""+fname+"\";filename=\"" + fname + "\"" + lineEnd);
                    outputStream.writeBytes(lineEnd);
    
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];
    
                    // Read file
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    
                    while (bytesRead > 0) {
                        outputStream.write(buffer, 0, bufferSize);
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    }
    
                    outputStream.writeBytes(lineEnd);
                    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
                    // Responses from the server (code and message)
                    int serverResponseCode = connection.getResponseCode();
                    String serverResponseMessage = connection.getResponseMessage();
                    InputStream in = connection.getInputStream();
    
                    byte data[] = new byte[1024];
                    int counter = -1;
    
                    in.close();
    
    
                    if (serverResponseCode == 200) {
                        Log.d("uploadFile", "File Upload Complete.");
                    }
    
                    fileInputStream.close();
                    outputStream.flush();
                    outputStream.close();
    
                }
            } catch (Exception ex) {
                Log.d("uploadFile", "File Upload Error:" + ex.toString());
    
                return false;
    
            }
    
    
            return true;
        }