Comments (8)
Hello guys,
I'm getting issue for same in Android 10,
as my file URI consist msf:
format.
Most solutions said to copy files into cache path & then use that path.
Thus, here is the solution to get a path, without copying it.
if (isAndroid10 && id.startsWith("msf:")) {
final String[] split = id.split(":");
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs);
}
Try to implement this.
MediaStore.Downloads.EXTERNAL_CONTENT_URI -- ONLY PRESENT FROM ANDROID 10
from whatthecodec.
And I forgot to say a big thanks to your excellent article.
from whatthecodec.
Hello @tamo ,
Thanks for kind words :)
As for the issue, I managed to reproduce it by downloading a video file via the Chrome app on Android 10 emulator. Unfortunately, just skipping the 'msf:' part doesn't help to make a proper Uri that could lead us to a file path. That is why I decided to just fallback to the File Descriptor in this case. The code change is already merged.
Thanks for pointing this thing out.
Soon I'll make a new release with this change.
from whatthecodec.
Thanks for the fix.
Yes, I downloaded movie files via Chrome. I should have mentioned it. Sorry.
For now your fix seems the only way to go. Googling MSF tells me only about medical information.
And I'm looking forward to see the new release.
from whatthecodec.
I tried this solution and its worked.
It simply copies the file for the application cache directory , then gets the path of the generated file.
if (id != null && id.startsWith("msf:")) {
final File file = new File(context.getCacheDir(), uri.getLastPathSegment());
try (final InputStream inputStream = context.getContentResolver().openInputStream(uri);
OutputStream output = new FileOutputStream(file)) {
// You may need to change buffer size. I use large buffer size to help loading large file , but be ware of
// OutOfMemory Exception
final byte[] buffer = new byte[8 * 1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
return file.getPath();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
P.S. You may need to clear the cache directory after finished
from whatthecodec.
@AliElDerawi , you are trying to solve just a slightly different problem here. The original issue was about having the prefix in the URI.
After that I acutually found a way of accessing files by a FileDescriptor (even if it isn't perfect).
Regarding copying the file to the cache, I wouldn't do such a thing. Original files can be very big (like movies) and actually I don't want to copy user's data, because it would lead to my Privacy Policy changing.
from whatthecodec.
@SanskarDahiya
I tried what you said:
String id = DocumentsContract.getDocumentId(uri);
if (id.startsWith("msf")){
final String[] split = id.split(":");
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
Log.e("Returned ", getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs));
}
The Uri
above is content://com.android.providers.downloads.documents/document/msf%3A16803
.
Here is my getDataColumn
:
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
The cursor
is null
so getDataColumn
returns null.
Is there something I'm doing wrong?
from whatthecodec.
@SanskarDahiya I tried what you said:
String id = DocumentsContract.getDocumentId(uri); if (id.startsWith("msf")){ final String[] split = id.split(":"); final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; Log.e("Returned ", getDataColumn(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI, selection, selectionArgs)); }The
Uri
above iscontent://com.android.providers.downloads.documents/document/msf%3A16803
.Here is my
getDataColumn
:private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } }catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); } return null; }The
cursor
isnull
sogetDataColumn
returns null.Is there something I'm doing wrong?
Hi @HBiSoft
You are doing everything fine. I'm also implementing the same code as mentioned by you.
Sill I'm facing an issue to get paths from some URI's in specific devices. I don't know what to do now.
Since I'm not an Android developer, So I kinda drop this issue.
from whatthecodec.
Related Issues (9)
- Sometimes some frames can't be decoded even with accessing by file:// HOT 3
- Don't limit file types by system HOT 1
- Unable to open from external storage (as file descriptor)? HOT 5
- Play video with it HOT 3
- Can't build the project HOT 1
- Couldn't open video
- Library not found HOT 1
- cannot get the library from Maven HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from whatthecodec.