Android选择拍照或视频文件上传
在Android应用程序中,我们经常需要让用户选择拍照或者视频文件上传到服务器。在这篇文章中,我们将介绍如何实现这一功能,并提供代码示例来帮助你快速上手。
实现步骤
- 添加相机和相册权限到AndroidManifest.xml文件中:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 创建一个布局文件,包含一个按钮用于选择照片或视频文件:
<Button
android:id="@+id/btnSelectFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择文件"
android:onClick="selectFile" />
- 在Activity中实现选择文件的逻辑:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_VIDEO_CAPTURE = 2;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFile(View view) {
final CharSequence[] options = {"拍照", "录像", "取消"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("选择文件来源");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (options[which].equals("拍照")) {
dispatchTakePictureIntent();
} else if (options[which].equals("录像")) {
dispatchTakeVideoIntent();
} else {
dialog.dismiss();
}
}
});
builder.show();
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
// Handle image captured
} else if (requestCode == REQUEST_VIDEO_CAPTURE) {
// Handle video captured
}
}
}
private Uri getOutputMediaFileUri(int type) {
File mediaFile = getOutputMediaFile(type);
return FileProvider.getUriForFile(this, "com.example.fileprovider", mediaFile);
}
private File getOutputMediaFile(int type) {
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyApp", "failed to create directory");
return null;
}
}
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
private static final int MEDIA_TYPE_IMAGE = 1;
private static final int MEDIA_TYPE_VIDEO = 2;
}
类图
classDiagram
MainActivity {
- static final int REQUEST_IMAGE_CAPTURE
- static final int REQUEST_VIDEO_CAPTURE
- Uri fileUri
+ onCreate()
+ selectFile()
+ dispatchTakePictureIntent()
+ dispatchTakeVideoIntent()
+ onActivityResult()
+ getOutputMediaFileUri()
+ getOutputMediaFile()
- MEDIA_TYPE_IMAGE
- MEDIA_TYPE_VIDEO
}
结尾
通过以上步骤和代码示例,你可以实现在Android应用中选择拍照或视频文件上传的功能。记得在AndroidManifest.xml文件中添加相应的权限,并根据需要修改文件保存路径。希望本文对你有所帮助,谢谢阅读!