Intent.ACTION_VIEW returning wrong file path - android

My app intends to launch a file using ACTION_VIEW.
The following code returns the file path of the selected file
if(Intent.ACTION_VIEW.equals(action)){
String Path = intent.getDataString();
//file processing code
}
It works fine when the selected file has no spaces in it. e.g Path becomes "/mnt/sdcard/sample.pdf" , but when i select a file with spaces in it's name such as "/mnt/sdcard/4C 1099 + 2 WOOO6.pdf" Path becomes "/mnt/sdcard/4C%20%20%201099%20%20%20%2B%20%202%20W0006.pdf"
Any help?

if(Intent.ACTION_VIEW.equals(action)){
Uri uri = intent.getData();
path = uri.getPath();
path = path.replace("%20", " ");
}

Related

How to open downloaded file using .openFileDescriptor()

I used to open a downloaded file using the following code :
final String filePath = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME));
if (TextUtils2.isNotEmpty(filePath)) {
apkFile = Uri.fromFile(new File(filePath));
}
startActivity(new Intent(Intent.ACTION_VIEW)
.setDataAndType(apkFile, UPDATE_APK_FILE_MIME_TYPE)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
But from API 24 DownloadManager.COLUMN_LOCAL_FILENAME is deprecated and I need to use openFileDescriptor (Uri uri,
String mode) method instead.
But I don't know how i can get the file path from the Fd to use it as data for the intent.
Does anyone know how to do it or whats the alternative to open downloaded file ?
To retrieve the good old absolute path of the downloaded file just :
String absolutePath = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).getPath();
And of course :
File file = new File(absolutePath);
instead of
cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME)
you can use
cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)
which return the uri for apk file you need

From Uri to Path and back to Uri

I’m using some big pictures that are stored in External Memory. I decode them, resize them and set the small bitmaps to various imageviews.
I keep track of those large images by storing their Uri’s. When the app stops, I convert Uri to paths and save them as Strings is a SQLite base:.
File myFile = new File(provider.getImageUri().toString());
cv.put(DBHelper.DB_IMAGEPATH, myFile.getAbsolutePath());
At this point the path to the image looks like:
/content/media/.. and everything works ok.
When the app resumes I read the path from database and convert it to Uri:
File tempFile = new File(cursor.getString(imagePathColIndex);
mUri = (Uri.fromFile(mFile));
Now the path in new mUri looks like:
File:///content/... And there is a IONotFound exception.
You should try with
File tempFile = new File(cursor.getString(imagePathColIndex));
mUri = Uri.parse(tempFile.getAbsolutePath());
Testing with the following code
String path = "/content/media";
Uri uri = Uri.fromFile(new File(path));
Log.e(TAG, uri.toString()); //print file:///content/media
uri = Uri.parse(path);
Log.e(TAG, uri.toString()); //print /content/media
EDIT
To read the image can you try
BitmapFactory.decodeStream(new FileInputStream(Uri.parse(path)));

Photo not saved in location given

I am trying to take a picture in my app, and then redisplay it. When I start the camera intent, I pass a URI to save the photo location in. Here is how I create the file:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "Waypoint_photo_" + timeStamp + ".jpg";
File storageDir = new File(PICTURE_FOLDER);
storageDir.mkdirs();
Log.v("FILE", String.format("New file: %s%s%s", PICTURE_FOLDER, File.separator, imageFileName));
return new File(storageDir, imageFileName);
Where picture folder is:
public static final String PICTURE_FOLDER = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "UtopiaWaypoints";
This creates a file similar to:
/storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_102116.jpg
In my onActivityResult, I get the file from the same path, and create a thumbnail version of it. At this point if I look on the device I see the files, but they are in /mnt/shell/emulated/0/Pictures/UtopiaWaypoints:
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ ls
Waypoint_photo_20150718_102825.jpg
Waypoint_photo_20150718_102825.png
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ pwd
/mnt/shell/emulated/0/Pictures/UtopiaWaypoints
On my device (nexus 6), /storage/emulated exists (neither is a symbolic link), but it only contains a "legacy" folder.
So when I try to load the file later from the /storage/emulated directory (which is what the Environment.PICTURE_DIRECTORY returns, the app can't find the file.
// This line outputs something similar to: /storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_1042581116411973.png
Log.v("WPA", String.format("Loading thumbnail from: %s", Utils.getFullThumbnailPath(wayPoint.getPicture())));
Bitmap imageBitmap = BitmapFactory.decodeFile(Utils.getFullThumbnailPath(wayPoint.getPicture()));
wayPointImageView.setImageBitmap(imageBitmap);
The second to the last line (decodeFile) shows this in the log:
D/skia﹕ --- SkImageDecoder::Factory returned null
imageBitmap is null and the ImageView is blank.
What am I doing wrong?

ACTION_GET_CONTENT : How to get file-ending when searching for multiple file types?

I am using an Intent to let the user select a file, and after the user has done that I want to know what kind of file-type the selected file is.
The intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
In my onActivityResult I want to extract the path from the intent through intent.getData() and all of it's "submethods" (getScheme(), getLastPathSegment() etc). However, I only get the Uri for the selected file.
Example of Uris':
Uri: content://media/external/audio/media/15185 //This is an audiofile
Uri: content://media/external/audio/media/20 //Another audiofile
Uri: content://media/external/images/media/20577 //this is a picture
Uri: file:///storage/emulated/0/testWed%20Apr%2017%2011%3A10%3A34%20CEST%202013 //This is a file
I've seen solutions of how to get the absolute path when the user is only allowed to chose images or audios. But how do I do I get the absolutePath (the real path with the name and file-ending, e.g. MyPicture.jpeg) if I want to allow the user to select from different file types?
The code I've been twiggling with to try to get the path-name in onActivityResult(int requestCode, int resultCode
String fileName = data.getData().getLastPathSegment().toString();
System.out.println("Uri: " +data.getData().toString());
File f = new File(fileName);
System.out.println("TrYinG wIWThA Da FiLE: " +f.getAbsolutePath());
System.out.println("FileNAME!!!: "+fileName);
By file-ending I'm assuming you mean the file extension? (i.e. mp4)
If so you can use the ContentResolver to get the mimetype:
String mimeType = getContentResolver().getType(sourceUri);
This will give you the mimetype assuming they are within the MediaStore which they should if you have the content Uri. so you'll get something like "image/png", "video/mp4", etc.
Then you can use MimeTypeMap to get the file extension from the mimetype:
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
Note that you should never substring the mimetype after "/" to get the extension as some mimetype does not use its extension such as ".mov" files which has the mimetype "video/quicktime"

android how to remove certain text from string?

I'm trying to allow user to rename file name, after taking picture, but I need to allow renaming to it right after saving the picture. So I want the name of the image to be shown in an EditText.
My saving code:
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Folder/cam_"+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(takePictureFromCameraIntent,1111);
String oriFileName = mUri.getPath();
My plan is : get the path with the name (oriFileName), then remove the front leave only the file name(cam_ ). But I have no idea on how to do so.
here you go:
String oriFileName = mUri.getPath();
oriFileName = oriFileName.substring(0, oriFileName.lastIndexOf("_"));

Categories

Resources