After recording the video
mMediaRecorder
.setOutPutFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
create the media file
private static File getOutputMediaFile(int type) {
File mediaStorageDir = newFile(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Pitch");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Pitch", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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;
}
I sent the video path with an intent to my video play activity to display the video
public void RecieveUri() {
Intent intent = new Intent(this,VideoPlayBackActivity.class);
vidPath = getOutputMediaFile(MEDIA_TYPE_VIDEO);
vidP = vidPath.getAbsolutePath();
intent.putExtra(EXTRA_MESSAGE,vidP);
startActivity(intent);
}
I received the file path checked if it was not empty
Intent intent = getIntent();
vidPATH = intent.getStringExtra(CameraActivity.EXTRA_MESSAGE);
if(vidPATH.isEmpty()){
Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this,"Path: "+vidPATH,Toast.LENGTH_SHORT).show();
}
Then i tried to play the video using media player
try{
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(vidPATH);
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
but it does not play the screen stays black and the log cat says
W/System.err: java.io.IOException: setDataSource failed.
Related
I am using intent camera to record videos. I have tested my app on Samsung, HTC, LG & Motorola Devices it works smoothly but on Nexus Phones its crashing after video is recorded and the file is selected for compression here is the code I am using to start intent and getting the video recorded file.
case R.id.camera_icon:
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
uri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
if (uri == null) {
// display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show();
} else {
videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// 0 = lowest res
startActivityForResult(videoIntent, RESULT_CODE_COMPRESS_VIDEO);
}
Creating Storage Location:
private Uri getOutputMediaFileUri(int mediaType) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
if (isExternalStorageAvailable()) {
// get the URI
// 1. Get the external storage directory
String appName = MainActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
// 2. Create our subdirectory
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e(TAG, "Failed to create directory.");
return null;
}
}
String path = mediaStorageDir.getPath() + File.separator;
if (mediaType == MEDIA_TYPE_IMAGE) {
mediaFile = new File(path + "thumb" + ".jpg");
} else if (mediaType == MEDIA_TYPE_VIDEO) {
mediaFile = new File(path + "video" + ".mp4");
} else {
return null;
}
Log.d(TAG, "File: " + Uri.fromFile(mediaFile));
// 5. Return the file's URI
return Uri.fromFile(mediaFile);
} else {
return null;
}
}
I am running a new Intent to access the camera in Android, but I'm getting this error:
getOutputMediaFileUri method not recognized
The code is below
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent,0);
Now this will store your camera captured images in MyImages directory in sdcard with image_001.jpg name.
Your code seems to come from this Android Camera tutorial.
It says there:
The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files.
And in that link, we find the code:
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
So add the above snippet to your code, and you should be good to go.
You haven't read correctly the article. You have to add below methods into your MainActivity.java
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*
* 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;
}
This is the method I use to store images.
private String saveImage (Context context, String name, Bitmap image){
ContextWrapper cw = new ContextWrapper(context);
File dirImages = cw.getDir(Imagenes, Context.MODE_PRIVATE);
File myPath = new File(dirImages, name + .png);
FileOutputStream fos = null;
try{
fos = new FileOutputStream(myPath);
image.compress(Bitmap.CompressFormat.JPEG, 10, fos);
fos.flush();
}catch (FileNotFoundException ex){
ex.printStackTrace();
}catch (IOException ex){
ex.printStackTrace();
}
return myPath.getAbsolutePath();
}
How do I adapt to store audio and video?
Try using this snippet of code:
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static final int MEDIA_TYPE_AUDIO = 3;
/** Create a File for saving an image, video or audio */
public static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "App");
// This locat ion works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
android.util.Log.d("log", "failed to create directory");
return null;
}
}
// Create a media file name
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ "PhotoTemp" + ".png");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ "VideoTemp" + ".mp4");
}
else if(type == MEDIA_TYPE_AUDIO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"AUD_"+ "AudioTemp" + ".mp3");
}
else {
return null;
}
return mediaFile;
}
And then:
File mediaFile = getOutputMediaFile(MEDIA_TYPE_AUDIO);
if (mediaFile == null) {
android.util.Log.d("log", "Error creating media file, check storage permissions");
return null;
}
FileOutputStream fos = new FileOutputStream(mediaFile);
fos.write(data); //where data is byte[]
fos.close();
While downloding from server which you probably do in chunks in a byte buffer, immediately write every chunk to file
I am attempting to use this reference:
https://www.parse.com/docs/android/guide#files
To understand how to get a file taken by the camera and save it to Parse. I have this code:
private File getOutputMediaFile(int type) {
// External sdcard location
String appName = CameraActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(appName, "Oops! Failed create "
+ appName + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
final File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
final ParseFile photoFile;
byte[] data = mediaFile.getBytes();
// Save the scaled image to Parse
photoFile = new ParseFile("profile_photo.jpg", data);
But I get the error: cannot resolve getBytes.
Do I need to convert this file to something else before using getBytes? Is it in the wrong format?
File haven't method getBytes(), you need to convert it to byte array, how is answered here : File to byte[] in Java
Here is my solution, based on this: http://www.mkyong.com/java/how-to-convert-file-into-an-array-of-bytes/:
private File getOutputMediaFile(int type) {
// External sdcard location
String appName = CameraActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(appName, "Oops! Failed create "
+ appName + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
final File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
final ParseFile photoFile;
FileInputStream fileInputStream=null;
byte[] bFile = new byte[(int) mediaFile.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(mediaFile);
fileInputStream.read(bFile);
fileInputStream.close();
for (int i = 0; i < bFile.length; i++) {
System.out.print((char)bFile[i]);
}
System.out.println("Done");
}catch(Exception e) {
e.printStackTrace();
}
// Save the image to Parse
photoFile = new ParseFile("profile_photo.jpg", bFile);
photoFile.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e != null) {
} else {
addPhotoToProfile(photoFile);
}
}
});
return mediaFile;
}
private void addPhotoToProfile(ParseFile photoFile) {
mCurrentUser.getCurrentUser();
mCurrentUser.put("ProfilePhoto", photoFile);
}
I am using built-in android intent in my camera app for video recording. My app can launch camera application and record video but as I click stop button of built-in camera app my app crashes and when check the directory where I save the videos, the recorded videos are stored there in the directory.
Here is my code please check it.
Button makeVideo = (Button) findViewById(R.id.makeVideo );
makeVideo.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
Uri fileUri = getOutputMediaFileUri(); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the video file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
}
});
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri()
{
return Uri.fromFile(getOutputMediaFile());
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile()
{
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath(), "My Videos");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists())
{
if (!mediaStorageDir.mkdirs())
{
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
return mediaFile;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_VIDEO_CAPTURED)
{
uriVideo = data.getData();
}
}
}
Here is my logcat
The API can't handle the path what you gave at
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
try with:
mediaFile = new File(mediaStorageDir.getPath() + "VID_" + timeStamp + ".mp4");
maybe it will solve it or do a simpler time format:
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_temp.mp4");
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
File.sepator is system-depent. File has a constructor that takes two paramters, a File and a String:
mediaFile = new File(mediaStorageDir, "VID_" + timeStamp + ".mp4");