我是AndroID开发的新手,我无法将视频压缩到AndroID应用程序中.
所以,请帮我以编程方式压缩视频并上传到服务器.
我使用FFmpeg但不知道如何正确使用它.请指导我这样做.
解决方法 客户端(ANDROID)代码package pack.coderzheaven;import java.io.DatainputStream;import java.io.DataOutputStream;import java.io.file;import java.io.fileinputStream;import java.io.IOException;import java.net.httpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import androID.app.Activity;import androID.content.Intent;import androID.database.Cursor;import androID.net.Uri;import androID.os.Bundle;import androID.os.Environment;import androID.provIDer.MediaStore;import androID.util.Log;public class UploadAudioDemo extends Activity { private static final int SELECT_AUdio = 2; String selectedpath = ""; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); opengalleryAudio(); } public voID opengalleryAudio(){ Intent intent = new Intent(); intent.setType("audio/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Audio "),SELECT_AUdio); } public voID onActivityResult(int requestCode,int resultCode,Intent data) { if (resultCode == RESulT_OK) { if (requestCode == SELECT_AUdio) { System.out.println("SELECT_AUdio"); Uri selectedImageUri = data.getData(); selectedpath = getPath(selectedImageUri); System.out.println("SELECT_AUdio Path : " + selectedpath); dofileUpload(); } } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedquery(uri,projection,null,null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.movetoFirst(); return cursor.getString(column_index); } private voID dofileUpload(){ httpURLConnection conn = null; DataOutputStream dos = null; DatainputStream inStream = null; String lineEnd = "rn"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead,bytesAvailable,bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; String responseFromServer = ""; String urlString = "http://your_website.com/upload_audio_test/upload_audio.PHP"; try { //------------------ CLIENT REQUEST fileinputStream fileinputStream = new fileinputStream(new file(selectedpath) ); // open a URL connection to the Servlet URL url = new URL(urlString); // Open a http connection to the URL conn = (httpURLConnection) url.openConnection(); // Allow inputs conn.setDoinput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection","Keep-Alive"); conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary); dos = new DataOutputStream( conn.getoutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-disposition: form-data; name="uploadedfile";filename="" + selectedpath + """ + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileinputStream.available(); bufferSize = Math.min(bytesAvailable,maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileinputStream.read(buffer,bufferSize); while (bytesRead > 0) { dos.write(buffer,bufferSize); bytesAvailable = fileinputStream.available(); bufferSize = Math.min(bytesAvailable,maxBufferSize); bytesRead = fileinputStream.read(buffer,bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close streams Log.e("DeBUG","file is written"); fileinputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Log.e("DeBUG","error: " + ex.getMessage(),ex); } catch (IOException ioe) { Log.e("DeBUG","error: " + ioe.getMessage(),ioe); } //------------------ read the SERVER RESPONSE try { inStream = new DatainputStream ( conn.getinputStream() ); String str; while (( str = inStream.readline()) != null) { Log.e("DeBUG","Server Response "+str); } inStream.close(); } catch (IOException ioex){ Log.e("DeBUG","error: " + ioex.getMessage(),ioex); } }}
服务器端(PHP)代码
<?PHP// Where the file is going to be placed$target_path = "uploads/";/* Add the original filename to our target path.Result is "uploads/filename.extension" */$target_path = $target_path . basename( $_fileS['uploadedfile']['name']);if(move_uploaded_file($_fileS['uploadedfile']['tmp_name'],$target_path)) { echo "The file ". basename( $_fileS['uploadedfile']['name']). " has been uploaded";} else{ echo "There was an error uploading the file,please try again!"; echo "filename: " . basename( $_fileS['uploadedfile']['name']); echo "target_path: " .$target_path;}?>
要记住的事情
1.确保您的服务器正在运行.
2.您的服务器文件路径应该是正确的.
3.检查服务器中的文件夹写入权限.
客户端代码上面是AUdio文件
你也可以将它用于图像,视频
通过做一些改变就好
intent.setType("audio/*");
根据您的要求使用视频/图像并使用它
用于压缩使用低于逻辑
我偶然发现的一个主要烦人的问题是我无法使用Intent向Google Mail应用发送多个附件.最快的方法当然是将所有文件压缩成一个(ZIP).
在网上搜索之后,我对你的AndroID设备上的压缩文件并没有太多了解 – 大多数文章都是针对标准的java应用程序,它假设你的所有文件都在你想要压缩的当前目录中.
所以,我尽我所能并掀起了我自己的包装类,它允许你轻松地在AndroID中压缩文件!
这是班级:
import androID.util.Log; import java.io.BufferedinputStream; import java.io.bufferedoutputstream; import java.io.fileinputStream; import java.io.fileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Compress { private static final int BUFFER = 2048; private String[] _files; private String _zipfile; public Compress(String[] files,String zipfile) { _files = files; _zipfile = zipfile; } public voID zip() { try { BufferedinputStream origin = null; fileOutputStream dest = new fileOutputStream(_zipfile); ZipOutputStream out = new ZipOutputStream(new bufferedoutputstream(dest)); byte data[] = new byte[BUFFER]; for(int i=0; i < _files.length; i++) { Log.v("Compress","Adding: " + _files[i]); fileinputStream fi = new fileinputStream(_files[i]); origin = new BufferedinputStream(fi,BUFFER); ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data,BUFFER)) != -1) { out.write(data,count); } origin.close(); } out.close(); } catch(Exception e) { e.printstacktrace(); } } }
如果这一切对你都有意义,那么你可能不会对我的解释感兴趣,否则,继续阅读. 总结
以上是内存溢出为你收集整理的android – 如何压缩视频并上传到服务器?全部内容,希望文章能够帮你解决android – 如何压缩视频并上传到服务器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)