我正在制作照片和视频共享Android应用,并使用Firebase作为我的数据库和存储。在我的ShareActivity中,用户可以选择图像捕获或录像机,并将其发送到数据库+存储。使用图像时一切正常但无法发送视频文件。 以下是我的ShareActivity代码
private static final String TAG = "Camera Tag";
private DatabaseReference mDatabaseShare;
private StorageReference mStorageShare;
private EditText mTitle;
private ImageView mTitleImage;
private Button bShare;
private Button mRecordBtn;
private Uri mFileUri;
private ProgressDialog mProgress;
private VideoView mVideoView;
// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Media Files";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
mTitle = (EditText) findViewById(R.id.title);
// imageview to start camera
mTitleImage = (ImageView) findViewById(R.id.titleimage);
// pushes data to firebase
bShare = (Button) findViewById(R.id.btnshare);
// Videoview for preview
mVideoView = (VideoView) findViewById(R.id.uservid);
// starts video recording
mRecordBtn = (Button) findViewById(R.id.buttonrecord);
mDatabaseShare = FirebaseDatabase.getInstance().getReference().child("SharedMedia");
mStorageShare = FirebaseStorage.getInstance().getReference();
mDatabaseShare.keepSynced(true);
mProgress = new ProgressDialog(this);
mTitleImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showImagePicker();
}
});
bShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareMedia();
}
});
mRecordBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recordVideo();
}
});
}
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
mFileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
// 1- for high quality video
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
private void showImagePicker() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mFileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
private Uri getOutputMediaFileUri(int mediaTypeImage) {
return Uri.fromFile(getOutputMediaFile(mediaTypeImage));
}
// Button click method to send data to firebase
private void shareMedia() {
final String name_title = mTitle.getText().toString().trim();
mProgress.setMessage("Please wait");
mProgress.show();
if (!TextUtils.isEmpty(name_title) && mFileUri != null){
StorageReference filepath = mStorageShare.child("Shared_Media").child(mFileUri.getLastPathSegment());
filepath.putFile(mFileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newShare = mDatabaseShare.push();
newShare.child("image").setValue(downloadUrl.toString());
newShare.child("video").setValue(downloadUrl.toString());
mProgress.dismiss();
startActivity(new Intent(ShareActivity.this,MainActivity.class));
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// video successfully recorded
// preview the recorded video
previewVideo();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT)
.show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT)
.show();
}
}
}
private void previewVideo() {
try {
mVideoView.setVideoPath(mFileUri.getPath());
// start playing
mVideoView.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void previewCapturedImage() {
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(mFileUri.getPath(),
options);
mTitleImage.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", mFileUri);
}
/*
* Here we restore the fileUri again
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
mFileUri = savedInstanceState.getParcelable("file_uri");
}
/*
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
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;
}