I'm trying to save a video to the gallery,the following code works well an all Android versions except the Android Q:
private void getPath() {
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = 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,
"Movies/" + "Folder");
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
Uri collection =
MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri videoUri = resolver.insert(collection, contentValues);
if (videoUri != null) {
try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(videoUri, "w", null)) {
OutputStream outputStream = null;
if (pfd != null) {
outputStream = resolver.openOutputStream(videoUri);
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
contentValues.clear();
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
if (videoUri != null) {
resolver.update(videoUri, contentValues, null, null);
}
} else {
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
+ "/Folder");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File videoFile = new File(storageDir, videoFileName);
savedVideoPath = videoFile.getAbsolutePath();
}
}
}
I also tried using get getExternalFilesDir() , but doesn't work, no video created in the gallery
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
File imageFile = null;
File storageDir = new File(
getExternalFilesDir(Environment.DIRECTORY_DCIM),
"Folder");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
imageFile = new File(storageDir, videoFileName);
}
savedVideoPath = imageFile.getAbsolutePath();
I use a 3rd-party library to record SurfaceView, this library requires a path to save the recorded video :
mRenderPipeline = EZFilter.input(this.effectBmp)
.addFilter(new GalleryEffects().getEffect(VideoMaker.this, i))
.enableRecord(savedVideoPath, true, false)
.into(mRenderView);
When record video finished, the recorded video saved with the given path savedVideoPath , everything works just fine on all android version except the Android Q
After saving the video, when I check, I get an empty video in the gallery
I have answered you to your other post to... You need an inputstream (file, bitmap etc.) and write an outputstream from the inputfile.
You have to change the library to make it work with Android Q . If you cannot do this you could copy the video to the media gallery and then delete the old video created in getExternalFilesDir(). After this you have the uri of the video and can do what you want with the uri
If you have saved the video with getExternalFilesDir() you could use my example here : The media uri you get is "uriSavedVideo" . This is only an example. A large video should also be copied in the background.
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 = mContext.getContentResolver();
Uri collection =
MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;
try {
pfd = mContext.getContentResolver().openFileDescriptor(uriSavedVideo, "w");
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
// Get the already saved video as fileinputstream from here
File storageDir = new File(
mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES),
"Folder");
File imageFile = new File(storageDir, "Myvideo");
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();
}
valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
mContext.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
I want to create a path to use it
You are getting a Uri from MediaStore. There is no "path". Not only can a Uri not be converted to a path, but you do not have filesystem access to that location on Android 10 and higher.
Get rid of this:
if (videoUri != null) {
savedVideoPath = getRealPathFromURI(videoUri);
}
as it will not work.
Replace it with your code to write out your video to the location identified by the Uri. Use resolver.openOutputStream() to get an OutputStream to that location. In particular, do this before you call resolver.update() for an IS_PENDING of 0, as that specifically says "I am done writing to the Uri; you can use the content now".
Or, use one of the filesystem locations that you do have access to, such as getExternalFilesDir() on Context, and get rid of the MediaStore stuff.
Related
I would like to move a file from the download folder to a folder in my app, I have seen that you can use the Files.move (source, destination) function, but I don't know how to get the source and destination path.
When y try
String sdCard = Environment.getExternalStorageDirectory().toString();
File ficheroPrueba = new File(sdCard + "/pau_alem18je_compressed.pdf");
if(ficheroPrueba.exists())
Log.v(TAG, "Hola")
}
Despite having downloaded the file (it is seen in the android emulator in downloads) it does not print the log.v
Use Environment.getExternalStorageDirectory() to get to the root of external storage (which, on some devices, is an SD card).
String sdCard = Environment.getExternalStorageDirectory().toString();
// the file to be moved or copied
File sourceLocation = new File (sdCard + "/sample.txt");
// make sure your target location folder exists!
File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");
// just to take note of the location sources
Log.v(TAG, "sourceLocation: " + sourceLocation);
Log.v(TAG, "targetLocation: " + targetLocation);
try {
// 1 = move the file, 2 = copy the file
int actionChoice = 2;
// moving the file to another directory
if(actionChoice==1){
if(sourceLocation.renameTo(targetLocation)){
Log.v(TAG, "Move file successful.");
}else{
Log.v(TAG, "Move file failed.");
}
}
// we will copy the file
else{
// make sure the target file exists
if(sourceLocation.exists()){
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copy file successful.");
}else{
Log.v(TAG, "Copy file failed. Source file missing.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
I am working now on ImageCompressor app.
I need to delete and write (update) image file. In Internal storage works perfectly but **SD Card can't give me access to Delete and Write files.
How can my app able to do write and delete operation on sd card (removable storage)?
I've already done whole project without this, so I have to must find a way.
Update:
I am already research & discuss about this issue. And understood I have to use storage access framework But I'm new on SAF.
I used a library to compress photo that need File not Uri. For that I do Uri -> File and use Intent.ACTION_OPEN_DOCUMENT and pick image from Removable storage.
But for removable storage I can't find Image Real Path from uri.
I don't know it's the right way or not. If there have any way in SAF where I can Compress my Image using uri, let me know. Or How to get Image real path from uri of Removable Storage Photo.
Update code SAF:
// ----------- Intent -------------
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
// ------------ On Activity Result --------------
Uri uri = data.getData();
try {
ParcelFileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fos = new FileOutputStream(fileDescriptor.getFileDescriptor());
FileInputStream fis = new FileInputStream(getImageFilePath(uri));
FileChannel source = fis.getChannel();
FileChannel destination = fos.getChannel();
destination.transferFrom(source, 0, source.size());
fis.close();
fos.close();
fileDescriptor.close();
Toast.makeText(this, "File save successfully.", Toast.LENGTH_SHORT).show();
}
Uri to File Path, I done where pick image from media apps(like Gallary, Photos) But pick from sd card what will instead of MediaStore.Images.Media.DATA I don't know. Code:
private File getImageFilePath(Uri uri) throws IOException {
String image_id = null, imagePath = null;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
image_id = cursor.getString(0);
image_id = image_id.substring(image_id.lastIndexOf(":") + 1);
cursor.close();
}
cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);
if (cursor!=null) {
cursor.moveToFirst();
imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
File file = new File(imagePath);
return new Compressor(this).setQuality(50).compressToFile(file);
}
Removable SD cards are write only on modern Android devices if you use File and FileOutputStream classes.
If you are lucky then your device using getExternalFilesDirs() returns as second item an app specific directory on the card where you still can write.
For the rest and instead use the Storage Access Framework.
Start with letting the user choose the sd card with Intent.ACTION_OPEN_DOCUMENT_TREE or a file with Intent.ACTION_OPEN_DOCUMENT.
I digged into an old Android app of mine and retrieved this :
This method convert the root of the sdcard uri, to a File path.
public File getRootPath(Context context, Uri sdcardRootUri)
{
List<String> pathSegments = sdcardRootUri.getPathSegments();
String[] tokens = pathSegments.get(pathSegments.size()-1).split(":");
for (File f : ContextCompat.getExternalFilesDirs(context, null))
{
String path = f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf("/Android/"));
if (path.contains(tokens[0]))
{
return new File(path);
}
}
return null;
}
And in order to retrieved the uri of the sdcard root, I used that :
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, SDCARD_ROOT_CODE);
Then the user would choose the root of the sdcard and then, I handled the result like this :
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK && requestCode == SDCARD_ROOT_CODE)
{
// Persist access permissions
Uri sdcdardRootUri = data.getData();
grantUriPermission(getPackageName(), sdcdardRootUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(sdcdardRootUri, takeFlags);
// Do whatever you want with sdcdardRootUri
}
}
I hope it's what you are looking for. With that you can read/write/delete any file you want on the sdcard.
you have try
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
//Filter common Linux partitions
if (line.contains("secure"))
continue;
if (line.contains("asec"))
continue;
if (line.contains("media"))
continue;
if (line.contains("system") || line.contains("cache")
|| line.contains("sys") || line.contains("data")
|| line.contains("tmpfs") || line.contains("shell")
|| line.contains("root") || line.contains("acct")
|| line.contains("proc") || line.contains("misc")
|| line.contains("obb")) {
continue;
}
if (line.contains("fat") || line.contains("fuse") || (line
.contains("ntfs"))) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
String path = columns[1];
if (path!=null&&!SdList.contains(path)&&path.contains("sd"))
SdList.add(columns[1]);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I know with Kit Kat you can only write to your applications package specific directory on SD Cards. I was however under the impression you could still copy files from an SD card to local storage with the:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am simply testing if I can copy one file. If I am able to do that I will add the code to search the entire SD card DCIM folder. For now I have the following code (please forgive the messiness of the code, I have written C# and vb.net but java is still very new to me):
String dirPath = getFilesDir().getAbsolutePath() + File.separator + "TCM";
File projDir = new File(dirPath);
if (!projDir.exists())
projDir.mkdirs();
String CamPath = projDir + File.separator + tv2.getText();
File projDir2 = new File(CamPath);
if (!projDir2.exists())
projDir2.mkdirs();
File LocalBuck = new File(projDir2 + File.separator );
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath();
File directory1 = new File (sdCard.getAbsolutePath() + "/DCIM");
File directory = new File(directory1 + "/100SDCIM");
File Buckfile = new File(directory, "/BigBuck.jpg");
try {
exportFile(Buckfile, LocalBuck);
} catch (IOException e) {
e.printStackTrace();
}
Here is my code for the export function/application:
private File exportFile(File src, File dst) throws IOException {
//if folder does not exist
if (!dst.exists()) {
if (!dst.mkdir()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HHmmss").format(new Date());
File expFile = new File(dst.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//Straight to Error Handler
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(expFile).getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
return expFile;
}
Here is what my emulator looks like:
Looking for: Debug of SD Location
Should Find It?: EmulatorShowingSd
Question: Am I even able to copy a file from the SD card to local storage after KitKat; if so what is wrong in the code causing the exception to be thrown when it tries to access the SD card file?
I am trying to set a .wav file as a ringtone on Android devices. The code below is able to create directories, and the sound file, but seems to have issues actually setting the file as a ringtone, let alone the selected ringtone. After the method ends, I go to ringtones on the device, and the selected ringtone defaults to "None". Any idea what's going on here? I am using the WRITE_EXTERNAL_STORAGE permission in my manifest. Also, the format of the sound bite doesn't matter to me, I don't mind converting anything that needs converted.
Thanks!!
private String saveAs(String fileName) {
int resSound = getContext().getResources().getIdentifier(fileName, "raw", getContext().getPackageName());
// Resolve save path and ensure we can read and write to it
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/audio/ringtones/";
File dir = new File(path);
fileName += ".wav";
if (!dir.exists()) {
dir.mkdirs();
}
if(!dir.canRead() || !dir.canWrite()) {
return "Unable to save ringtone.";
}
// Load the audio into a buffer
byte[] buffer;
InputStream fIn = this.context.getBaseContext().getResources().openRawResource(resSound);
int size;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
}
catch (IOException e) {
return "Error opening sound file";
}
File file = new File(dir, fileName);
FileOutputStream save;
try {
save = new FileOutputStream(file);
save.write(buffer);
save.flush();
save.close();
}
catch (FileNotFoundException e) {
return "Error loading sound file.";
}
catch (IOException e) {
return "Unable to save ringtone.";
}
// Register the sound byte with the OS and set its properties
this.context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path + fileName)));
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, getSoundTitle(fileName));
values.put(MediaStore.MediaColumns.SIZE, size);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, "Sound Clip");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = this.context.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), values);
RingtoneManager.setActualDefaultRingtoneUri(this.context, RingtoneManager.TYPE_RINGTONE, uri);
return "Successfully set ringtone.";
}
For anyone else who runs into this, I figured it out. It's this line.
this.context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path + fileName)));
If anyone happens to know why this is, I'd be very interested to know. Thanks!
I am new to android and programming. I am using the android touchpaint api, and using the following code to save a drawing, but obviously saving is useless without being able to open the file.
I was wondering if anyone could help me with some code for it.
// Function saves image to file
public String save(Bitmap mBitmap, boolean showMsg) {
String filename;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
filename = sdf.format(date);
try {
String path = Environment.getExternalStorageDirectory().toString() + "/modTouchPaint/";
File file1 = new File(path);
file1.mkdirs();
OutputStream fOut = null;
File file = new File(path, filename + ".jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
if (showMsg)
Toast.makeText(this, "Picture saved to " + path + filename + ".jpg", 9000).show();
return path + filename + ".jpg";
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Please make sure that SD card is installed", 5000).show();
return null;
}
}
Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
See the documentation for more information.