Android 不同类型的视频和图像
我正在做一些应用程序。不过,我已经完成了两个模块,它们调用手机的本机摄像头来拍摄快照和录制视频。我打算使用手机应用程序将手机拍摄和录制的图像和视频发送到我打算创建的网站。但是,对于文本信息,我可以将信息存储为字符串,对于图像和视频,我不确定是否应该在提交时将它们保留为URI。下面是我的照片和视频节目。塔克斯 图片代码:Android 不同类型的视频和图像,android,Android,我正在做一些应用程序。不过,我已经完成了两个模块,它们调用手机的本机摄像头来拍摄快照和录制视频。我打算使用手机应用程序将手机拍摄和录制的图像和视频发送到我打算创建的网站。但是,对于文本信息,我可以将信息存储为字符串,对于图像和视频,我不确定是否应该在提交时将它们保留为URI。下面是我的照片和视频节目。塔克斯 图片代码: package com.project; import java.io.File; import android.app.Activity; import android.c
package com.project;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyPicture extends Activity {
/** Called when the activity is first created. */
/*constant and variable created so as to work with the taken pictures*/
private static int TAKE_PICTURE = 1;
private Uri outputFileUri;
Uri imageUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic);
Button pictureButton=(Button) findViewById(R.id.pictureButton);
pictureButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
if (requestCode == TAKE_PICTURE){
imageUri = data.getData();
//do something about the image in the in outputFileUri
Toast.makeText(MyPicture.this,
"Picture successfully taken",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyPicture.this,/*program execution proceeds back to MyPicture, our start page after success of image takin*/
Myindex.class);
startActivity(i);
}else{
Toast.makeText(MyPicture.this,
"Picture Unsuccessfully taken",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyPicture.this,/*program execution proceeds back to MyPicture, so we can redo the recording*/
MyPicture.class);
startActivity(i);
}
}
}
视频代码:
package com.project;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyVideo extends Activity {
/*program for the vid button*/
private static int RECORD_VIDEO = 1;
private static int HIGH_VIDEO_QUALITY = 1;
//private static int MMS_VIDEO_QUALITY = 0;
Uri recordedVideo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vid);
Button videoButton=(Button) findViewById(R.id.videoButton);
videoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, HIGH_VIDEO_QUALITY);
startActivityForResult(intent, RECORD_VIDEO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == RECORD_VIDEO){
recordedVideo = data.getData();
//to do something with the recorded video
//we shall insert this information in the database
Toast.makeText(MyVideo.this,
"Video successfully recorded",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyVideo.this,/*program execution proceeds back to Myindex, our start page*/
Myindex.class);
startActivity(i);
}
else{
/*Happens after unsuccessfull recording of video*/
Toast.makeText(MyVideo.this,
"Video Unsuccessfully recorded",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MyVideo.this,/*program execution proceeds back to MyVideo, so we can redo the recording*/
MyVideo.class);
startActivity(i);
}
}
}
对于图像,我通常将数据解码为
位图
,然后使用多部分内容类型通过HTTPPOST发送
您可以使用以下命令将图像文件解码为位图
:
下面是一个示例,说明如何使用库通过多部分发送位图
:
公共字符串doHttpMultipart(字符串url,
列出对,
位图,
字符串文件名)引发IOException,
客户协议例外,
不支持的编码异常{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
compress(CompressFormat.PNG,100,bos);
字节[]imageData=bos.toByteArray();
ByteArrayBody ByteArrayBody=新的ByteArrayBody(图像数据,文件名);
多端口实体=
新的多端口(HttpMultipartMode.BROWSER_兼容);
REQUENTITY.addPart(“图像”,byteArrayBody);
对于(NameValuePair p:pairs){
reqEntity.addPart(p.getName(),新StringBody(p.getValue());
}
HttpPost请求=新的HttpPost(url);
请求。setEntity(reqEntity);
HttpClient=new DefaultHttpClient();
HttpResponse HttpResponse=client.execute(请求);
字符串响应=”;
BufferedReader in=null;
试一试{
response=super.readHttpStream(response,in,httpResponse);
}捕获(非法状态){
抛出新的非法状态异常();
}捕获(IOE异常){
抛出新IOException();
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
返回响应;
}
对于视频文件,它应该是相同的,您应该了解如何将其解码为字节数组并使用多部分发送
public String doHttpMultipart(String url,
List<NameValuePair> pairs,
Bitmap bitmap,
String fileName) throws IOException,
ClientProtocolException,
UnsupportedEncodingException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] imageData = bos.toByteArray();
ByteArrayBody byteArrayBody = new ByteArrayBody(imageData, fileName);
MultipartEntity reqEntity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", byteArrayBody);
for(NameValuePair p : pairs) {
reqEntity.addPart(p.getName(), new StringBody(p.getValue()));
}
HttpPost request = new HttpPost(url);
request.setEntity(reqEntity);
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
String response = "";
BufferedReader in = null;
try {
response = super.readHttpStream(response, in, httpResponse);
} catch(IllegalStateException e) {
throw new IllegalStateException();
} catch(IOException e) {
throw new IOException();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response;
}