Delete file using uri extracted from MediaStore.Audio.Media.DATA - android

Before marking this question as duplicate i wanna say that i had visited many similar questions but none of them had solved my problem.So, i had extracted all audio files from internal storage using MediaStore and stored its Uri in string form but later when i try to use file.delete using that Uri to delete the audio, former is returning false.
Following goes the code for extracting song uri and its working properly:
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor c=getContentResolver().query(uri,null,selection,null,null);
if(c!=null)
{
c.moveToFirst();
do {
String audioName=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String url=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
String size=c.getString(c.getColumnIndex(MediaStore.Audio.Media.SIZE));
audioInfoArrayList.add(new AudioInfo(url,audioName,artist,album));
}while (c.moveToNext());
c.close();
Here, i had stored all the retrieved information about audio in arrayList of a custom type which also stores above extracted uri in string form which could be extracted using getUrl() method.
Following goes the code how i'am trying to delete it:
Uri uri= Uri.parse(audioInfo.get(position).getUrl()); //its actually returning the url stored above in string form
deleteFile(uri.getPath());
Log.i("logText","uri.getPath() : "+uri.getPath());
Log.i("logText","audioInfo.get(position).getUrl() : "+audioInfo.get(position).getUrl());
deleteFile function:
public void deleteFile(String filePath){
if(filePath.startsWith("content://")){
ContentResolver contentResolver = context.getContentResolver();
contentResolver.delete(Uri.parse(filePath), null, null);
}else {
File file = new File(filePath);
if(file.exists()) {
if (file.delete()) {
Log.e("logText", "File deleted."); //condition 0
}else {
Log.e("logText", "Failed to delete file!"); //condition 1
}
}else {
Log.e("logText", "File not exist!"); //condition 2
}
}
}
And finally my logcat for a sample audio file:
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer E/logText: Failed to delete file!
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: uri.getPath() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: audioInfo.get(position).getUrl() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3
Note: On calling deleteFile() condition 1 is executed not 2 or 0 which according to me means that file is being located using that uri in the storage but can't be deleted and file.delete() is returning false.

Requesting for read and write external storage permission in manifest is not enough for Android 6 and above.
You should add code to ask the user of your app to confirm the requested permissions.
Google for runtime permissions.

Related

Get file name from Uri in Android

I have an Uri build using below code
final Uri fileUri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId);
that contains value:
content://com.android.externalstorage.documents/tree/primary%Sample/document/primary%Sample%2FMedia%2F.Hide%2FScreenshot_1615959401.png/children
Below is my code to access the name and mime type of file:
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(fileUri, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE}, null, null, null);
try {
if (cursor != null && cursor.moveToFirst())
{
//Here cursor.moveToFirst() returns false
}
} catch(Exception e)
{
e.printStackTrace();
}
I want to access the name of file like Screenshot_1615959401.png, mime type file like image/png from above Uri but cursor.moveToFirst() returns 0. Why record is not found even Uri is valid. Because I try to use this Uri to load image and ImageView is displaying the image correctly using above Uri.
Any Help would be highly appreciated as I spent lots of time googling and read on SO but didn't find any solution or root cause of it.
Below is my code to access the name and mime type of file:
That Uri is not for a document. It is for the roster of child documents for a document tree, where the tree is identified by docId. childDocumentsUri is the core of the name of the method that you are calling.
Why record is not found even Uri is valid
It is not valid for your desired action.
If you wish to build a Uri for a document, given a document ID and a tree, use buildDocumentUriUsingTree(), not buildChildDocumentsUriUsingTree().

Andriod 10 How to check file exist

In Android Q, save pictures in app-specific directory,
path like = /data/user/0/xxx.xxx.xxx/files/phone/abc.jpg
not save in the external storage, use Device FileExplorer to view,
need to check if file exist, avoid to download again
,but in Android Q file.exist() not work
File newFile = new File(path);
newFile.exists();
always return false
this question. I need to use MediaStore or SAF to resolver it.
or other function to check it.
If I use MediaStore to check. use ContentResolver. May be like this:
public void getPhotoCursor(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
But I can't get the Uri form app-specific directory. If I get the Uri, how to use file descriptor to check.
or use SAF to check.
File testFile = new File(getExternalFilesDir()+"phone", "abc.jpg");
FileProvider.getUriForFile(,,testFile);
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
testIntent.setDataAndType();
startActivity(testIntent);
In the ActivityResult to check it
Any help will be apperciated
is my fault, every time open APP I will delete all the .jpg from APP-specific.
so into APP I want to check avoid download again. file exist always return false.

Android: delete image on sd card

Android 7.0, API 24. Permissions are granted in both AndroidManifest and real time.
Following are the scenarios which I have tried:
Only using the content resolver removes it from MediaStore but it comes back when the device is restarted.
When deleting an internal image "/storage/emulated/0/DCIM/Camera/..."
it works, but when trying to delete an external image (or what should
be external image) "/storage/4ED7-7F17/DCIM/Camera/..." it fails at
file.canWrite().
Using Environment.getExternalStorageDirectory().getAbsolutePath() returns "/storage/emulated/0" (+ "/DCIM/Camera/...") and it fails at file.exists().
Hardcoding "/SD card/DCIM/Camera/..." (which should be the correct filepath) fails at file.exists().
Weirdly, in the Android Device File Explorer, the files that should be in the SD Card folder are in the "/storage/4ED7-7F17/" folder which the files have a permission listing of -rwxrwx--x. And permission inside "/storage/emulated/" is "Permission denied". But to find the files in Android app MyFiles or Windows File Explorer the files are in "/SD card/DCIM/Camera/...".
Completely confused any help would be greatly appreciated.
File file = new File(filename);
if (file.exists()) {
if (file.canWrite()) {
if (file.delete()) {
// Set up the projection (we only need the ID)
String[] projection = {MediaStore.Files.FileColumns._ID};
// Match on the file path
String selection = MediaStore.Files.FileColumns.DATA + " = ?";
String[] selectionArgs = new String[]{filename};
Uri queryUri;
if (isVideo) {
// Query for the ID of the media matching the file path
queryUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else {
// Query for the ID of the media matching the file path
queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
ContentResolver contentResolver = context.getContentResolver();
Cursor c = contentResolver.query( queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID));
Uri deleteUri = ContentUris.withAppendedId(
MediaStore.Files.getContentUri("external"), id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
Toast.makeText(context, "File not found: " + filename,
Toast.LENGTH_LONG).show();
}
c.close();
Toast.makeText(context, "File deleted: " + filename,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "File not deleted: " + filename,
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(context, "File cannot be wrote to: " + filename,
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(context, "File does not exist: " + filename,
Toast.LENGTH_LONG).show();
}
}
) "storage/4ED7-7F17/DCIM/Camera/..." it fails at file.canWrite().
Yes of course. Micro SD cards are read only on modern Android systems.
Apps nowadays can only write in one app specific directory on SD card.
If you want write access to the whole card you need to use the Storage Access Framework.
I stand correct. What's missing on N is MediaStore.getDocumentUri(Context context, Uri mediaUri), which provides conversion of fictitious file paths for the DCIM directory on Oreo+ to a Storage Access Framework URI that can be used to delete the file. N doesn't seem to provide an equivalent that I can find. And the content: Uris for media files don't work with Storage Access Framework unless converted.

QuickOffice: get file uri from share function of quickoffice

In my app I can download a document (docx for example) and open it in QuickOffice. After edditing the document I use the save button and after succesfully saved it, I hit the share button and select my app so I can reupload it.
My problem is, is that the uri i got is not the usual uri you would expect as content://storage/map/file.docx or something like that. I get this from quickoffice:
content://com.quickoffice.android.quickcommon.FileContentProvider/zEV5qmvBJOg2GGWldHMJnNK687Ur6qLGbbMbxj0IxV9cDv2mN8XTGqRrEqU4KIfeZuQNMKMJ_eDx%0AN4YiNZwDShhb4E8%3D%0A
My question is, how can I turn this uri to the real path uri from the file (content://storage/map/file.docx for example)
There is no "real path".
A ContentProvider is welcome to store its content wherever it wants, which may not be a file (e.g., BLOB column in a database) and, even if it is, it may not be a file which you can access (e.g., internal storage for the app hosting the ContentProvider.
Please use the various methods on ContentResolver, such as openInputStream(), to access the contents of this provider.
Please use the following code. it worked fine for me.
public static String getContentName(ContentResolver resolver, Uri uri){
String[] ATTACHMENT_META_COLUMNS = {
OpenableColumns.DISPLAY_NAME,
OpenableColumns.SIZE
};
String name = "";
int size= 0;
Cursor metadataCursor = resolver.query(uri, ATTACHMENT_META_COLUMNS, null, null, null);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
name = metadataCursor.getString(0);
size = metadataCursor.getInt(1);
}
} finally {
metadataCursor.close();
}
}
if (name == null) {
name = uri.getLastPathSegment();
}
return name;
}

Fetching attachments with custom extension in my Android application

What I am trying to achieve is sounds very familiar, it has been posted many times here and there in Stack Overflow as well, but I'm unable to get it done.
The scenario is, I receive a mail with attachment having custom extension in it. The extension is recognized by my app and it needs the FilePath to process it.
Currently, when I get the attachment in my app using getIntent().getData() all I get is path of the form content://
I have seen methods to convert media content of the type content:// to FilePath like /sdcard/file.ext but I was unable to convert the attachment using that. May be its obvious.
Is there any way that I can process the content:// type without actually downloading it.
Currently from the k9 mail app, when I get the custom extension, it shows my app in the list and opens it through it, but I need FilePath like /sdcard/file.ext and I'm only able to get content:// type.
I hope I made the question clear.
Please Help.
Regards.
A content:// Uri does not necessarily point to a file on the sdcard.
It is more likely that it points to any kind of data stored in a database
or to a content provider that gives you access to the private file storage of another app.
I think the later one is the case with mail attachments (if the content provider is not requesting it directly from a web server). So converting the content:// Uri to a path will not work.
I did the following (not sure if it works also for k9 mail app)
Uri uri = intent.getData();
if (uri.getScheme().equals("content")) {
String fileName = ContentProviderUtils.getAttachmentName(this, uri);
if (fileName.toLowerCase().endsWith(".ext")) {
InputStream is = this.getContentResolver().openInputStream(uri);
// do something
} else {
// not correct extension
return;
}
} else if (uri.getScheme().equals("file")) {
String path = uri.getPath();
if (path.toLowerCase().endsWith(".ext")) {
InputStream is = new FileInputStream(path);
// do something
} else {
// not correct extension
return;
}
}
The attachment name can be found by
public static String getAttachmentName(Context ctxt, Uri contentUri) {
Cursor cursor = ctxt.getContentResolver().query(contentUri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
String res = "";
if (cursor != null){
cursor.moveToFirst();
int nameIdx = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
res = cursor.getString(nameIdx);
cursor.close();
}
return res;
}

Categories

Resources