I would like to know how to get a file from its name.
I use the camera to take a photo. It saved the photo with this:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
And now (after closing and opening again the app), I would like to get the file or Uri from the name of the picture (I saved it in a string (timeStamp)) to a preview.
Sorry for my english.
Related
In Mi device when i captured image from my custom camera image save perfectly to sdcard but after few minutes image name automatic change
For save image in sdcard i have used below code
java.util.Date date = new java.util.Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(date.getTime());
file = new File(file.getAbsolutePath() + File.separator
+ "IMG_"
+ timeStamp
+ ".jpg");
image name save as in sdcard with following format :
IMG_1601464_142653.jpg
After few minutes image name change with IMG_1601464_142653_152145632141214.jpg
i don't know from where _152145632141214 this line is added at last of image name in MI device
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);
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.?
I'm making an app that let's you either capture photo/video, or choose an existing photo/video before sending. I set up the directory to save the files in here:
String appName = Main.this.getString(R.string.app_name);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), appName);
and I name the files here
File mediaFile;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if(mediaType == MEDIA_TYPE_PHOTO){
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
} else if(mediaType==MEDIA_TYPE_VIDEO){
mediaFile = new File(path + "VID_" + timestamp + ".mp4");
}
So it's saving my pictures under /storage/emulated/0/pictures/(app name) with correct timestamped format. However, my videos are being saved to /storage/emulated/0/DCIM/100MEDIA and are just being named VIDEO0073, VIDEO00074, etc. I tried changing the directory name to MOVIES instead of PICTURES or DCIM, but there is no effect. I'm on an HTC One running Android 4.3
This is a bug that occurs on certain devices including the HTC One. If you include your code where you declare your video Intent I could more precisely answer the question, but basically, after you declare your Intent for the video (not image), get your Uri like so:
videoUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new ContentValues());
In this case the video will be saved in the /storage/emulated/0/video folder instead of the pictures or dcim folders you mentioned above.
I try to rename video file with this code:
File from = new File(outputFileName);
File to = new File(mediaStorageDir,mediaFile);
from.renameTo(to);
when
outputFileName = //mnt/sdcard/Movies/Your_voice/Your_voice.mp4
and
mediaFile = mediaStorageDir.getPath() + File.separator
+ "Your_voice" +
timeStamp +
".mp4";
and
mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
"Your_voice");
no error in locat .... but not execute rename.
I thought this problem cause because this file play in videoview so before the code I add videoView.setVideoPath(""); but it dont help, what I need to do ???
thanks ahead...
You already specify the mediaStorageDir when constructing the File object:
File to = new File(mediaStorageDir,mediaFile);
so you should remove the mediaStorageDir.getPath() from mediaFile, as follows:
mediaFile = "Your_voice" + timeStamp + ".mp4";
You should probably also remove the Your_voice part from the mediaStorageDir, just use:
mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);