I am storing video in internal storage but the saved video is not showing up in gallery. To see that first i have to open my file manager then come back to gallery. I stored the same video in android Q using media store but in
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "videoFileName");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MOVIES);
Uri finalUriPath = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
File sourceLocation = new File(finalUri.getPath());
InputStream in = new FileInputStream(sourceLocation);
FileOutputStream out = new FileOutputStream(String.valueOf(finalUriPath));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
contentValues.clear();
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
StatusShowerActivity.this.getContentResolver().update(finalUriPath, contentValues, null, null);
Toast.makeText(StatusShowerActivity.this, "Saved", Toast.LENGTH_SHORT).show();
Exception got from Catch in android 9
java.io.FileNotFoundException: null (Read-only file system)
Related
I'm trying to copy some files from my App's internal folder to the external memory of an android device.
I'm using ACTION_OPEN_DOCUMENT_TREE for the user to select the desired destination folder, in 'to', and have a list of Uri for the files I want to copy 'from', what's the best way to do it?
I tried:
public void copyFiles(ArrayList <Uri>from, Uri to)
{
for (Uri u:from
) {
try {
File f = new File(u.getPath());
FileInputStream in = new FileInputStream(f);
File d = new File(to.getPath(), f.getName());
FileOutputStream out = new FileOutputStream(d);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
}
catch (Exception ex){
Log.e(TAG,ex.getLocalizedMessage());
}
}
}
But it throws an exception complaining the destination file does not exist: open failed: ENOENT (No such file or directory).
Am I missing some step?
I saved the file as below
I want to change the file name but I want to know how
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename + ".wav");
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/*");
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MUSIC + "/" + "dir" + "/");
Uri uri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
File cacheDir = getCacheDir();
File cacheFile = new File(cacheDir, filename + ".wav");
FileInputStream fis = new FileInputStream(cacheFile);
OutputStream outputStream = getContentResolver().openOutputStream(uri);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
fis.close();
I tried referring to the article below, but without success
Do you have any way? thank you
Rename file of the Mediastore which is created by app in android 10. Working on Android API 30 but shows error in API 29
Having recorded a video using mediaRecorder and Camera API, I am trying to save the video to the user's gallery (either by saving to their external movies, DCIM directory, or other).
Unfortunately, the code I've found from the docs or StackOverflow seem to create their respective URIs without any reference to the recorded video or the file in which its kept. Specifically, the code I attempted to use is:
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
ContentValues valuesvideos;
valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Folder");
valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = context.getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;
try {
pfd = context.getContentResolver().openFileDescriptor(uriSavedVideo,"w");
assert pfd != null;
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
// Get the already saved video as fileinputstream from here
File storageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "Folder");
File imageFile = new File(storageDir, "testStorage");
FileInputStream in = new FileInputStream(imageFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
pfd.close();
} catch (Exception e) {
e.printStackTrace();
}
The code and answers around it seem to imply that by accessing the MediaStore.VOLUME_EXTERNAL_PRIMARY the resolver can access the saved video file but I see no explicit reference to it. Prior to recording the video, I define the file assigned to mediaRecorder as such:
static File getMediaFile(Context c) {
File mediaStorageDir = c.getExternalFilesDir(null);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
}
Upon stopping the video I get this error (referencing this line: Uri uriSavedVideo = resolver.insert(collection, valuesvideos);):
java.lang.UnsupportedOperationException: Unknown URI: content://media/external_primary/video/media at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java.167) ...
How do I create a URI properly and with reference to the saved video? Substituting the URI from file Uri.fromFile(f); in place of uriSavedVideo doesn't work either.
To save a specific video file to gallery, I passed the file as a parameter to new FileInputStream.
Secondly, as the Android docs state:
Only your app can view the file until your app changes the value of IS_PENDING back to 0.
So in keeping with user #blackapps' advice, I manually changed that state. The changed code is below:
private static void addToApi29Gallery(File file, Context context) {
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
ContentValues valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Trickshott");
valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = context.getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); //all video files on primary external storage
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;
try {
pfd = context.getContentResolver().openFileDescriptor(uriSavedVideo,"w");
assert pfd != null;
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
// Get the already saved video as fileinputstream from here.
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
pfd.close();
valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0); // Only your app can see the files until pending is turned into 0.
context.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
Im trying to convert an audio file to byte array. where the audio file is fetch through attach file so its in an Uri
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
//FileInputStream = reading the file
fis = new FileInputStream(new File(audioFxUri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
i followed that code from this one but once i ran. It gave me an error
W/System.err: java.io.FileNotFoundException: /external/audio/media/646818: open failed: ENOENT (No such file or directory)
I also tried to play it with a MediaPlayer and place Uri as its Source
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(AttachAudio.this,audioFxUri.toString());
player.prepare();
player.start();
to make sure if the audio file is working or not. Ps. it worked.
Is there something wrong im doing as to why it didnt convert to byte array?
The File Uri you have /external/audio/media/646818 is not an actual File Path, hence it is giving FileNotFoundException.
You need to get absolute path from the URI and then read the file accordingly.
You can either use below code:
public String getRealPathFromURI(Uri contentUri)
{
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
or Refer this or this SO link for other options/methods
I got a camera application where, after the user takes an image, I save it to the internal storage in a directory I have made.
All that works fine, the image gets saved there and I can load it and show afterwards, but I'm having trouble saving the image to the Android gallery too.
What I want to, is after saving the image to the internal directory, copy it to the gallery.
I have tried this:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.SIZE, file.length());
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Where the file is the image I saved in internal storage.
With this method all I get is a broken image in the gallery.
copy it to gallery or show it in android gallery?
if you want the image show in android gallery? you can do this by scan media, this is how to scan:
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(scanFileIntent);
if you want just copy the file just do this :
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
You can use this method for saving image:
public void saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/");
storagePath.mkdirs();
File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
This is what might help :
Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));
After getting the path u can store the image by this :
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.SIZE, file.length());
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(imageFile, values);
Hope this helps.