How to save video file in SD card in android - android

I am trying to save Video file on SD card but getting null pointer.
Have a look of my code.
I just need to create a folder in SD Card and save videos on it.
When i have not using fileUri then not got Crash.
File mediaFile = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/myvideo"+System.currentTimeMillis() +".mp4");
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, VIDEO_CAPTURE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VIDEO_CAPTURE) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(getActivity(), "Video has been saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getActivity(), "Video recording cancelled.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Failed to record video",
Toast.LENGTH_LONG).show();
}
}
}
Thanks.
Suggestion appreciated.

To Save File on SD Card Follow the code.
add "android.permission.WRITE_EXTERNAL_STORAGE" in manifest file."
And then use the code -
filePath = getExternalFilesDirs("/")[1].toString();
OutputStreamWriter writer;
File file;
File folder = new File(filePath);
if (!folder.exists()) {
folder.mkdirs();
}
file = new File(filePath + "yourfilename.txt");
writer = new OutputStreamWriter(ostream, "UTF-8");
//save your video file here
writer.close();

Try this,
private static File getOutputMediaFile(int type){
// Check that the SDCard is mounted
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraVideo");
// Create the storage directory(MyCameraVideo) if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
output.setText("Failed to create directory MyCameraVideo.");
Toast.makeText(ActivityContext, "Failed to create directory MyCameraVideo.",
Toast.LENGTH_LONG).show();
Log.d("MyCameraVideo", "Failed to create directory MyCameraVideo.");
return null;
}
}
// Create a media file name
// For unique file name appending current timeStamp with file name
java.util.Date date= new java.util.Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(date.getTime());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO) {
// For unique video file name appending current timeStamp with file name
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
Refer this link,
http://androidexample.com/Camera_Video_Capture_And_Save_On_SDCard_-_Android_Example/index.php?view=article_discription&aid=123
I wish it will help you.

Related

How to set the output of a file as mp4 using intent to a camera with a action of ACTION_VIDEO_CAPTURE?

when I am using my native app camera to take video, the output file have 3gp extension. I need to intent to camera with ACTION_VIDEO_CAPTURE intent action which will produce a file that has a mp4 file extension. how can I do it?
You can go ahead and try dis code:
intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO); // create a file to save the video in specific folder (this works for video only)
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);
This will be called once the capture is completed
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_VIDEO_CAPTURED_NEXUS:
this.videoFromCamera(resultCode, data);
break;
default:
break;
}
}
}
private void videoFromCamera(int resultCode, Intent data) {
if(fileUri != null) {
Log.d(TAG, "Video saved to:\n" + fileUri);
Log.d(TAG, "Video path:\n" + fileUri.getPath());
Log.d(TAG, "Video name:\n" + getName(fileUri));
// use uri.getLastPathSegment() if store in folder
//use the file Uri.
}
}
Get the output Media file uri with the following Method
public Uri getOutputMediaFile(int type)
{
// To be safe, you should check that the SDCard is mounted
if(Environment.getExternalStorageState() != null) {
// this works for Android 2.2 and above
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");
// 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(TAG, "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_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return Uri.fromFile(mediaFile);
}
return null;
}
This will save the captured video in pure MP4 format.
You can add the following code before start the intent :
videoUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" + String.valueOf(System.currentTimeMillis()) + ".mp4"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);

getOutputMediaFileUri method not recognized in android camera intent

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;
}

Saving file into internal storage and make the files accessible to the user

I am making a camera app and I wanted to save the photos taken into the internal storage. I wish to make all the photos taken accessible to the user, not only for my app. I think I should not use the File class by android. Any suggestion?
In your onClick call capturePicture(): and copy paste below code
private void capturePicture()
{
if (isCameraPresent())
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, Global.CAMERA_CAPTURE_REQUEST_CODE);
}
}
private File getOutputMediaFile()
{
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Global.IMAGE_DIRECTORY_NAME);
// Global.IMAGE_DIRECTORY_NAME is String in Global value is abc/images
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists())
{
if (!mediaStorageDir.mkdirs())
{
Log.d(Global.IMAGE_DIRECTORY_NAME, "Oops! Failed create " + Global.IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "CAPTURE" + ".jpg");
return mediaFile;
}
private boolean isCameraPresent()
{
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;// this device has a camera
else
return false;// no camera on this device
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == Global.CAMERA_CAPTURE_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
// Image is saved into internal pictures folder >> abc folder>>Images
}
else
{
// failed to capture image
Toast.makeText(getApplicationContext(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
}
}
}
This will save captured image in internal storage/Pictures/abc/images
Hope this is what you want and this helps you :)

Why my app crashes when I stop video recording?

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");

Android video capture intent crashes

I need to record a video within the application using the intent. I think the code is correct, however when I do start the image stops and the file gets 0Bytes. I let down the code I'm using.
The first function called:
protected void makeVideo() {
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
getOutputMediaFile function:
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
// Environment.getExternalStorageState();
// File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), Constants.ALBUM.AlbumInPhone);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Constants.ALBUM.AlbumInPhone);
// 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(Constants.ALBUM.AlbumInPhone, "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;
}
File x = mediaFile;
int y=0;
y=1;
return mediaFile;
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
and:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
// Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
String uri = fileUri.getPath();
if(TextUtils.isEmpty(uri)){
Log.v(TAG, "Could not get video");
} else {
Log.v(TAG, "video: " + uri);
if(BebeDataBaseManager.addVideoToAlbum(this, uri, albumId)){
Log.v(TAG, "video: added");
this.cursor.requery();
} else {
Log.v(TAG, "video: not added");
}
}
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
...
I'm doing something wrong?

Categories

Resources