android - Get a Content URI from a File URI? -
I am using an image download in the system's gallery and then in the broadcast receiver (once downloading Download is successful) intent to set image as wallpaper
Everything was working fine but recently on 4.4 I got an exception in the photo / Google + app because This is expecting a content URI and the file is not a URI.
So my question is, does anyone know how to convert a full file path / URI (file: //) into content style URI (content: //)?
The lack of source code, I'm away from the computer that is the source, but I hope the question comes without understanding, obtain the content style uri with a full path.
Edit: / Strong> The image has been copied to the system's gallery or media gallery, which has not been saved in the internal store of my app. Here's an example that I want to convert: to edit 2: < : Main 04-21 10: 50: 35.090: E / Android Random (7220): Process: com.google.android.apps.plus, PID: 7220 04-21 10: 50: 35.090: E / Android Renta Im (7220): java.lang.RuntimeException: Not eligible to resume activity {com.google.android.apps.plus/com.google.android.apps.photos.phone.SetWallpaperActivity}: java.lang .IllegalArgumentException: Image should be URI content plan type Here is the code that I use to set the user to wallpaper: where I was able to understand it. It was a combination of code found here: after downloading the media is scanning the file. Therefore, after downloading the file, I am getting the path and do the following: For any reason by starting a media scanner, updating the file, and getting a full path is important, I'm not sure why, but I can not spend much time on this I am! The method for converting a file from URI to Content URI is as follows (The linked stack is taken from overflow post: It may help someone in the future.
file: ///storage/emulated/0/Pictures/Rockstar/image.jpg
content: // media / internal / images / media / 445
string uriString = C.getString (c.getColumnIndex (DownloadManager.COLUMN_LOCAL_URI)); Uri U = Uri. Pars (Yuri String); Intention wall_intent = new intent (Intent.ACTION_ATTACH_DATA); Wall_intent.setDataAndType (U, "image / *"); wall_intent.putExtra ("mimetype", "image / *"); Intimate Chaser Intent = Intestine. Content Choice (Vol_content, "Set Edge"); ChooserIntent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); Try {Context.startActivity (chooserIntent); }
Yuri string is :
file: /// storage / emulated / 0 / picture / rockstar / image . Jpg
string uriString = c.getString (c.getColumnIndex (DownloadManager.COLUMN_LOCAL_URI)); // System Yuri U = Uri.Pars (Yuri String) Update; Context.sendBroadcast (new intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, U)); // Get the abs path using a file, this is important file wallpaper_file = new file (u.getPath ()); Uri content uri = getImageContentUri (reference, wallpaper_file.getAbsolutePath ());
< code> public static URI getImageContentUri (reference reference, string absPath) {log v (TAG, "getImageContentUri:" + absPath); cursor cursor = context.getContentResolver () .query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new string [ ] {MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?", New string [] {absPath}, tap); if (cursor! = Null & cursor.movefitfest ()) {Int id = cursor.getInt (cursor.getColum nIndex (MediaStore.MediaColumns._ID); Return Uri.withAppendedPath (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString (id));} and if (! absPath.isEmpty ()) {ContentValues Value = New ContentValues (); Values.put (MediaStore.pdf.media.dat A, Bespath); return context.getContentResolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, value);} and {return tap;}} < / Code>
Comments
Post a Comment