Saving camera api videos in internal storage directory - android

I am building an app which record videos with camera API. I followed a tutorial from android developers site after the video records, It is saved in an external storage directory. How do I save in internal storage instead of external storage.
public static final int MEDIA_TYPE_VIDEO = 2;
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private 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), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
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;
if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}

All you need is in the official android dev site: https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Related

store video file on external sdcard in ionic cordova

I have done coding for take video using Media capture plugin and I am also using File Plugin and File transfer plugin. I have done with taking video and store in internal storage in specific folder.
I am doing this in both android and IOS.
Now, what I want, is there any way to check weather device has external sd card inserted or not and then save video on sd card instead of internal storage.
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;
}
Follow this link for your reference, this link will help you to store video file on external sdcard in ionic cordova.
http://androidexample.com/Camera_Video_Capture_And_Save_On_SDCard_-_Android_Example/index.php?view=article_discription&aid=123
I hope you solve your problem very soon.

Can't record camera mp4 on External storage in android post 4.3

I have a permission exception when I try (I think) to create a file in my physical device's external storage (nexus 5 android 6.0), whereas it works fine with a 4.3 emulated device.
My permissions are fine, but I get this error :
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.VIDEO_CAPTURE flg=0x3 cmp=com.google.android.GoogleCamera/com.android.camera.VideoCamera clip={text/uri-list U:file:///storage/emulated/0/Pictures/MyCameraApp/VID_20151216_145322.mp4} (has extras) } from ProcessRecord{5dab894 22712:com.***} (pid=22712, uid=10199) with revoked permission android.permission.CAMERA
any idea ?
I'll add a little more information :
My media utils is basic copy/paste from google video tutorial :
public class MediaUtils {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
/** Create a file Uri for saving an image or video */
public static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
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), "MyCameraApp");
// 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;
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;
}
}
And my intent :
Uri fileUri = MediaUtils.getOutputMediaFileUri(MediaUtils.MEDIA_TYPE_VIDEO);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
There is a new permission system on API 23 and higher.
The permission is requested at run time.
You should read this How to manage permission on Android 6.0 and this Android request permission

Android: storing video file captured by the camera

Hello I'm developing an app which uses the camera2 API. I used the Caemra2Video sample provided by Google, and during the modification I changed the file storage directory.
The original code is like this:
mMediaRecorder.setOutputFile(getVideoFile(activity).getAbsolutePath());
...
private File getVideoFile(Context context) {
return new File(context.getExternalFilesDir(null), "video.mp4");
}
I changed it in this way:
private File getVideoFile(Context context) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "camera2Video");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File videoFile;
videoFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
return videoFile;
}
If I record a video, there is indeed a file with the correct name inside the folder. However, whenever I open the app and start the camera activity, there will be a .mp4 file generated (no matter whether I actually record the video or not) with the size of 0. How can I modify it to avoid this? Thanks!

Android: When I save a file in the phone's internal memory will store Where's that file? How can I access it path?

we have 2 memories for store files:
1- phone memory
2- sd card memory
now :
I can access to files and roots in sdcard memory with Environment.getExternalStorageDirectory ... and return : /storage/sdcard0 this is ok.
but :
When i store files and media (music,pic, ...) in phone memory, how can to get path phone memory?
There are different ways of storing and retrieving application data; SharedPreferences, Internal Storage, External Storage.
- SharedPreferences basically restricts other apps from accessing the data.
- Internal Storage basically stores the data in Phone memory
- External Storage basically stores the data in any other memory cards which can be mounted or unmounted.
If I understand your question properly, then you are interested in External Storage - Public files which basically stores the files in External storage and allows any other apps / user to access the file.
For example:
File sample = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
For app's internal file directory, use
getFilesDir();
For external storage, use
Environment.getExternalStorageDirectory();
Okay, I usually use this helper class to save my media files:
/**
* This class create, name given the timestamp and save a multimedia file in the Environment.DIRECTORY_PICTURES folder
* This location works best if you want the created images to be shared
* between applications and persist after your app has been uninstalled.
*/
public class FileManager {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
public static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), VideoRecordingActivity.TAG);
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;
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;
}
}
Then to use it I will just do:
File currentFile= FileManager.getOutputMediaFile(2);
Note: The media file will be created in sdcard/Pictures/AppName but to be able to see them with your file browser make sure to update your mediaScanner:
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.getPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, final Uri fileUri) {
//Eventually some UI updates
}
});

Mysterious NullpointerException after the built-in camera app saves my video properly

I have an activity that allows you to record a video if you open a dialog and click on an icon.
The problem is that after I stop recording it throws a NullPointerException even though the video is saved properly. According to Log Cat the error is not in my code so I tried to place "checkpoints" in my code and I found out that even the onActivityResult of my activity is executed properly so now I'm out of idea what to do.
Here is the Log Cat:
Code:
these are from my dialog that invokes the camera app
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
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
((Activity)context).startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
private static Uri getOutputMediaFileUri(int type)
{
return Uri.fromFile(getOutputMediaFile(type));
}
private 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.getExternalStorageDirectory()+"/Movies", "MyCameraApp");
// 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;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + "/" +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + "/" +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
This code was more or less copied from the android developers site.
As I mentioned even the onActivityResult of my activity is executed properly(where I dismiss the dialog) after this.
Try this:
private static File getOutputMediaFile(int type)
{
File mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
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());
String mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = "IMG_"+ timeStamp + ".jpg";
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = "VID_"+ timeStamp + ".mp4";
} else {
return null;
}
return new File(mediaStorageDir, mediaFile);
}
The getExternalStorageDirectory method returns a File object, not a string you can append a subdirectory to.
It also wonder if the directory returned by that method would be available to a service. The Android specs say:
On devices with multiple users (as described by UserManager), each
user has their own isolated external storage. Applications only have
access to the external storage for the user they're running as.
LOL!!!! Just realized this question was asked 2 years ago! Did you find the answer yet?? LOL

Categories

Resources