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);
}
Related
I tested this code with Genymotion Marshmallow and with Nougat on my HTC 10, and it worked on both.
Now I tried Android 7.0 on Genymotion and it didn't create the directories.
Any idea why?
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
boolean isDir = file.exists();
if (!isDir)
isDir = file.mkdirs();
if (isDir) {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}
else
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
Edit: My manifest contains these lines:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your code the Toast will be shown even when creating the directory worked. The Activity will only be started when the file already existed before calling your code.
Try this:
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
if(!file.exists()) {
if(file.mkdirs()) {
startNotesManager();
} else {
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
}
} else {
startNotesManager();
}
And implement this helper method for starting the Activity:
private void startNotesManager() {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}
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);
Decoding image file received from Media.EXTRA_OUTPUT in action_pick intent produce file not found exception. I see a lot of similar questions regarding to this but still can't figure out the issue. I face this issue in android Gallery app below Lollipop and the same issue in Google photo app above android version >= 5.
Myfragment.java
if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Helpers.getOutputMediaFileUri());
Intent startImage = Intent.createChooser(chooseImageIntent, "Select From");
startImage.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[]{takePictureIntent});
((Activity) mContext).startActivityForResult(startImage, Constants.REQUEST_CHOOSE_FROM);
} else {
((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}
Helpers.java
public static String LAST_IMAGE_FILE;
public static final String TEMP_IMAGE_FILE = "TEMP_IMG";
public static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"In.Touch");
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 = new File(mediaStorageDir.getPath() + File.separator
+ TEMP_IMAGE_FILE + "_" + timeStamp + ".jpg");
LAST_IMAGE_FILE = mediaFile.getAbsolutePath();
return mediaFile;
}
OnActivityResult
File file = new File(Helpers.LAST_IMAGE_FILE);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
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 };
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?