Android video file path from media store is coming as null - android

I am trying to get the path of the video file for a video thumbnail. I'm not sure why it is still coming as null after I modified based on some solutions here. The version of android is 6.0.1.
The user clicks the floating action button and summons a gallery of videos.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.addNote);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
}
});
When the user selects a desired video from the gallery, the video goes to the activity which it'll be sorted out.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.d(TAG, "Uri: " + uri);
Log.d(TAG, "Uri authority: " + uri.getAuthority());
String filemanagerstring = uri.getPath();
Log.d(TAG, "filemanagerstring: " + filemanagerstring);
String selectedImagePath = getPath(uri);
Log.d(TAG, "selectedImagePath: " + selectedImagePath);
}
}
The method to get the path of the video file.
public String getPath(Uri uri) {
Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
int idx = 0;
//Source not from device capture or selection
if (cursor == null) {
return uri.getPath();
} else {
cursor.moveToFirst();
idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
if (idx == -1) {
Log.d(TAG, "uri path: " + path);
return uri.getPath();
}
}
String path = cursor.getString(idx);
Log.d(TAG, "path: " + path);
cursor.close();
return path;
}
Results: I got the null (-1) and got the uri's path, that's not the correct path. I need the full path of the video file.
Uri: content://com.android.providers.media.documents/document/video%3A6174
Uri authority: com.android.providers.media.documents
filemanagerstring: /document/video:6174
**uri path: 16842794**
selectedImagePath: /document/video:6174

and summons a gallery of videos
No, it does not. It allows the user to choose from any activity that supports ACTION_GET_CONTENT for a MIME type of video/*. The Uri that you get back can be from anything, not necessarily a "gallery" app, and not necessarily one that points to a file. The Uri could point to:
A file on external storage, one that you might be able to read directly
A file on removable storage, which you cannot access
A file on internal storage of some other app
The contents of a BLOB column in a database
Something that has to be decrypted on the fly
Something that does not yet exist on the device and needs to be downloaded
And so on
The method to get the path of the video file
The only values you can get back from that query(), reliably, are the OpenableColumns, for the size and "display name" of the content.
You need to either:
Use a thumbnail engine that accepts a content Uri as a parameter, or
Use ContentResolver and openInputStream() to get an InputStream on the content, then use some thumbnail engine that accepts an InputStream as a parameter, or
Use ContentResolver and openInputStream() to get an InputStream on the content, then use that stream to make your own file that contains a copy of the bytes from the content, so you can use your own file with some thumbnail engine that requires a file, or
Do not use ACTION_GET_CONTENT, but instead render your own "chooser" UI by asking the MediaStore for all videos, as you can get thumbnails of those videos from MediaStore (see this sample app)

Related

android filename from uri is not the same as actual filename

I need to get the name and extension of a file that I need to upload. I let the user select the file using an Intent call and get the URI as follows:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/* stuff */
Uri uri = data.getData();
String filePath = data.getData().getPath();
Log.d("filePath ",filePath);
Log.d("URI ", uri.toString());
String fileName = (new File(filePath)).getName();
Log.d("fileName ",fileName);
but the results are as follows:
com.blah.blah D/URI: content://com.android.providers.downloads.documents/document/354
com.blah.blah D/filePath: /document/354
com.blah.blah D/fileName: 354
the name of the file isn't even 354! its a PDF file (say "Bus e-Ticket.pdf" or "Xender.apk")
String fileName = (new File(filePath)).getAbsolutePath();
also yields the same.
how can i get the file name/exstension on disk?
but the results are as follows
getPath() is pointless on a Uri with a content scheme. getPath() is pointless on a Uri with any scheme other than file.
the name of the file isn't even 354
There is no requirement that there be a file. https://stackoverflow.com/questions/46060367/android-filename-from-uri-is-not-the-same-as-actual-filename is a Uri, and I feel fairly confident that there is no file on a Stack Overflow server at the path /questions/46060367/android-filename-from-uri-is-not-the-same-as-actual-filename.
how can i get the file name/exstension on disk?
There is no requirement that the user choose something that is a file. You can use DocumentFile.fromSingleUri() and getName() to get a "display name" for the content identified by the Uri. That does not have to be a filename with an extension.
You can use the MediaStore class to obtain the filename seen by the user. In specific use the MediaColumns interface as shown below
String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME};
ContentResolver cr = mctx.getContentResolver();
Cursor metaCursor = cr.query(uri[0], projection, null, null, null);
if (metaCursor != null) {
try {
if (metaCursor.moveToFirst()) {
realFileName = metaCursor.getString(0);
}
} finally {
metaCursor.close();
}
}

Read .txt file from anywhere on the phone

In my app I have this code allowing the user to select a file :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent,1);
The user can select the .txt file from anywhere in his phone, even from google drive. When the file selection is done I retrieve a Uri object corresponding to the file. The problem is I can't use this Uri to read the file because it is not valid. Here is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
File file = new File(uri.toString());
try{
InputStream inputStream = new FileInputStream(file);
int content;
while ((content = inputStream.read()) != -1) {
Log.d("===>", String.valueOf((char) content));
}
}catch (Exception e){
Log.d("===>", e.toString());
}
}
}
}
I always get a fileNotFoundException. My question is, is there a way to read the selected file (without knowing in advance the location it will come from). And if not, is there a way to copy the selected file in a folder from which I would easily get it ?
The problem is I can't use this Uri to read the file because it is not valid.
That is because a Uri is not a file.
is there a way to read the selected file (without knowing in advance the location it will come from)
The user did not select a file. The user selected a piece of content.
To consume the content represented by the Uri, call openInputStream() on a ContentResolver, passing in the Uri. This gives you an InputStream that you can use to read in the content.

ImageView not populating from file path

I can't get my ImageViews to update from either URI or file path - they just don't show an image
Intent to capture image:
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photo, 1);
On ActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Uri imageUri = data.getData();
filePath = getRealPathFromURI(this, imageUri);
}
GetRealPathFromURI class:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
It then inserts 'filePath' from onActivityResult in to a db
Retrieving from db and updating ImageViews
imgfilepath[y] = cursorc.getString(cursorc.getColumnIndex("IMAGE"));
imgFile[y] = new File(imgfilepath[y]);
Uri uri = Uri.fromFile(imgFile[y]);
String path = uri.getPath();
mImage.setImageURI(uri);
I've tried so many different ways to setImageBitmap etc which haven't worked (I can't remember all of them) - Can anyone see why this is not showing the image?
The image is in emulated storage and not the SD card.
EDIT:
I've added EXTRA_OUTPUT tag but I can't see the image in DDMS anywhere & the camera does not exit after taking the picture/accepting the image
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
mImageUri = Uri.fromFile(sdImageMainDirectory);
photo.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(photo, 1);
Uri imageUri = data.getData();
ACTION_IMAGE_CAPTURE does not return a Uri.
The image is in emulated storage
Perhaps that one camera app does, in which case that camera app has a bug, to go along with the return-a-Uri bug.
There are thousands of Android device models. These ship with hundreds of different camera apps pre-installed, and there are hundreds more available from the Play Store and elsewhere. Many will have ACTION_IMAGE_CAPTURE implementations. Most should follow the documented protocol. None should save the image for your request, because you did not tell the camera app where to save the image.
Either:
Provide a location, via EXTRA_OUTPUT, for the camera app to save the image to, then load the image from that location, or
Use data.getExtra("data") to get a Bitmap that represents a thumbnail-sized image, if you do not provide EXTRA_OUTPUT

Android Picking Sound File Path - Full Path Not Returned

I am trying to get the full path of a sound file on the SD card.
This launches sound picker - I then use the Play Music app to select a file
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_SOUNDPICKER);
On activity result I am trying to get the full path
case RESULT_SOUNDPICKER: {
Log.d("TAG", "onActivityResult "+requestCode+" "+resultCode);
if (resultCode == RESULT_OK)
{
Uri uri = data.getData();
String filePath = uri.getPath();
Log.d("TAG", "FilePath: "+filePath);
// A song was picked.
Log.d("TAG", "PickSongActivity.onActivityResult: "+data.getDataString());
}
}
But this returns a path like
//media/external/audio/media/13085
Rather than a proper path of where the file is held.
I need to get the full path back as I then want to use it to play the file.
Thank you.
Solution
This method can be used to get the full path.
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
I need to get the full path back as I then want to use it to play the file.
There may not be a path (as it does not have to be a file), let alone a path that you can reach (as the file does not have to be on storage that is accessible to you).
MediaPlayer can use a Uri directly, so I suggest going that route.

Length of image picked from gallery via intent

I have a problem. I want to make file uploader app. I'm using Intent.ACTION_GET_CONTENT to pick any file from my device. I am receiving the file in onActivityResult like that:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
final Uri uri = data.getData();
File file = FileUtils.getFile(uri);
String fileName = file.getName();
String filePath = file.getPath();
int fileSize = Integer.parseInt(String.valueOf(file.length()/1024));
tvName.setText(fileName);
tvPath.setText(filePath);
tvSize.setText(String.valueOf(fileSize) + "kB");
}
}
}
I want to show user information about picked file. In general everything is fine, but when I choose Image from gallery then:
The file name shows no format - it's a number (probably reference to file in memory). When I pick Image via installed file explore I get sth like imagename.png etc, but picked from gallery is like "195493"
File.length created from data picked from gallery is 0.
Is there any way to access real image name and size after picking image from Gallery via intent?
Just input URI from the intent and get the size of any file
uri = data.getData();
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
Log.e("TAG", "Name:" + returnCursor.getString(nameIndex));
Log.e("TAG","Size: "+Long.toString(returnCursor.getLong(sizeIndex)));
I think this will help you.
When picking images from the gallery, you don't get a reference to the actual file but to a row in the database. You have to retrieve the actual file path with a Cursor like in this answer.

Categories

Resources