getOutputMediaFileUri method not recognized in android camera intent - android

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

Related

How to play a video file from external storage

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.

How to save video file in SD card in 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.

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

getoutputmediafileuri method is not accessible?

I'm learning how to take a picture and save it's path into a file.
According to the tutorials offers on android developers website, the method
getoutputmediafileuri() is used, however, when I tried to use that method, i found that it's
not accessible or undefined, i mean eclipse underlines this method with redline. I don't know
how to fix this error.
Please find below the code
public class SaveCameraImageDemoActivity extends Activity {
/** Called when the activity is first created. */
Button btn01;
private Uri fileURI;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn01 = (Button) findViewById(R.id.btn01);
btn01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intenet = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileURI = getoutputmediafileuri();
//intenet.putExtra("output", uri.getPath());
startActivityForResult(intenet,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}
There is no inbuilt method getoutputmediafileuri() in android.
It is a custom method someone write for getting file URI to store captured images in particular directory. You have to defined and put logic for it. Instead of that use this code,
EDIT:
btn01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
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.
The getoutputmediafileuri() method is defined here: http://developer.android.com/guide/topics/media/camera.html#saving-media
Now the document has added the code. Just paste the at the end of your Class, it work well for me. Code See As Below.
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 */
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.
// 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;
}

android camera saving, but not showing up on the sd card

I am working on adding a camera to my application, I have basically decided to go with adding an intent to it, and using the built in camera. The intent works, it calls it just fine, but when it goes to save it gets weird. according to Eclipse the photos are saving to the SD card, I can see them and pull them off when i go under FileExplorer. However, when I actually go to and explore the SD card, the files are not there.
public class C4Main extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private Camera cam;
private SurfaceHolder sh;
private Uri fileUri;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("testing","before intent");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(1); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
Log.d("testing","fileUri: "+fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
Log.d("testing","after startactivity");
}
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.
// 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 == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
Log.d("testing loop","Filepath: "+mediaFile.getPath());
} else if(type == 2) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
that is the code that I am using, copied from developer.google. And as I said according to FileExplorer in Eclipse it is saving to the designated path, but it just is not on my SD card. My hardware is ASUS Transformer prime running android 4.0.3.
Tha manifest is set-up with permission for both external writing and camera use.
Any help would be greatly appreciated.
I'm using this intent for using built-in camera. It automatically stores the image in SD card.
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
flagImage=1;
startActivityForResult(cameraIntent, CAMERA_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Cursor c1 = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
null, p1[1] + " DESC");
if (c1.moveToFirst()) {
String uristringpic = "content://media/external/images/media/"
+ c1.getInt(0);
Uri newuri = Uri.parse(uristringpic);
// Log.i("TAG", "newuri "+newuri);
String snapName = getRealPathFromURI(newuri);
Uri u = Uri.parse(snapName);
File f = new File("" + u);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.PNG, 0 /* ignored for PNG */,
bos);
byte[] bitmapdata = bos.toByteArray();
// Storing Image in new folder
StoreByteImage(mContext, bitmapdata, 100, fileName);

Categories

Resources