Issue With Playing mp4 and 3gp from server on VideoView In Application - android

I have an application where videos are recorded using the inbuilt camera, and then uploaded to a webserver. The videos are saved in mp4 and 3gp formats.The code excerpts to take the videos are:
// 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());
String mediaFile2;
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
preview_uri=mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg";
pathname="IMG_" + timeStamp + ".jpg";
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".3gp");
preview_uri= mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".3gp";
pathname="VID_" + timeStamp + ".3gp";
} else {
return null;
}
I upload them using a php script , and i use filezilla to confirm they are actually in the "video" folder. Also, typing in the address in my web browser, opens a download dialog to begin download of the file.
However, when i want to play the file in the application, it fails with the error message :
"sorry, this video is not valid for streaming to this device"
This error is the same for mp4 and 3gp files. The code for the videoview is:
video.setMediaController(new MediaController(this));
video.setVideoPath(path);
video.requestFocus();
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
video.start();
}
});
Also, no errors display in logcat and the videos play with vlc media player on my laptop. How can i resolve this?

Related

Deleting Video File in Android

I want to the delete the file after compression is done. I have used the code to delete the file. But when I check in the Gallery the video file is still there but it doesn't play shows error Media not Supported. Here is my code.
if (compressed) {
snackbar = TSnackbar
.make(coordinatorLayout,"Video Compressed Successfully",TSnackbar.LENGTH_SHORT);
snackbar.show();
//Delete File from Location.
File videoFile = new File(mediaFile.getPath());
if(videoFile.exists())
{
boolean del = videoFile.delete();
}
}
This is mediaFile
mediaFile = new File(path + "VID_" + timestamp + ".mp4");
This is the storage directory
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
Try this
getContentResolver().delete(Uri.parse(mediafile.getPath()),null,null);

How to get path of selected image

Currently i am taking image from camera and gallery . When i take picture from camera then the path is storage/sdcard0/pictures/androidfileupload/.
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Android File Upload");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
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 {
return null;
}
return mediaFile;
}
so from the above code i am fetching storage/sdcard0/pictures/android file upload/ .
Now i am taking selected image from gallery so it shows error like storage/sdcard0/pictures/androidfileupload/imagename.jpg is not found .
I have debug the code and i get the path of selected image from gallery content://media/external/images/media/26435 but i want the path like
content://media/external/images/media/imagename.jpg.
so how can i get the path with imagename.jpg.?

Trouble creating directory

I am trying to create a directory to save images inside.
my function to achieve creating the directory:
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, "Fehler beim Erstellen der Datei: "
+ 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 {
return null;
}
return mediaFile;
}
I have added the permission to my AndroidManifest file:
But I get the following issue:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.Integer.intValue()' on a null object reference
The file path my app is generating:
/storage/emulated/0/Pictures/ImageDokumentation
Why is mkdirs() not working ?
Some little mistakes in your code, try this
Replace your first part of code with this
// External sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + IMAGE_DIRECTORY_NAME };

How to resize the image before saving in Android

I want to save image in a directory, but before I save it I want to reduce the size of the image as I need to Upload it later. How can I resize the image and save it?
private static File saveImage(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 {
return null;
}
return mediaFile;
}
is there any library or inbuilt function to do it?
You need to load the image and resize it.See BitmapFactory and you can use decodeFile() to load the image and resize it with BitmapOptions, and Bitmap class has a method to save the scaled image to disk. See compress

Unable to create new directory inside the Gallery folder

I am making an app which allows users to record audio clips and access them. I am trying to save the recorded audio files inside a custom folder in my gallery.
I have a function that creates a File and sets its location:
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "dirName" + File.separator);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
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 if (type == MEDIA_TYPE_AUDIO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"AUD" + timeStamp + ".WAV");
}
else {
return null;
}
return mediaFile;
}
I use getOutputMediaFile to save recorded audio file like so:
audioRecorder = new MediaRecorder();
audioFilename = getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath();
//more code
audioRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_AUDIO).getAbsolutePath());
When I log audioFilename, I get the following path:
/storage/emulated/0/DCIM/folderName/AUD20160425_172620.WAV
I am able to save and play the audio file, but with a caveat:
The file is NOT stored in gallery/myFolder; I can only view the audio file if I run the default play music app and look inside "my playlists".
I have the following permissions in my manifest:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
What am I doing wrong?
I guess we need to tell MediaStore that "a new file is created". Try this:
public void refreshGallery(File file, Context context) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
scanIntent.setData(contentUri);
context.sendBroadcast(scanIntent);
}

Categories

Resources