convert parse.com file uri to string Android - java

i have a file that i got back from parse.com and i am trying to get the string stored in that file to a textView. At the moment all i am getting is a url in my text view and not the string i need.
getting the parse file:
ParseFile file = message.getParseFile("file");
Uri fileUri = Uri.parse(file.getUrl());
//passing it to another class
intent.setData(fileUri);
getting the file to display in textview
mDisplay = (TextView)findViewById(R.id.messageDisplay);
Uri textUri = getIntent().getData();
// i have tried this also [ File string = new File(textUri.toString()); ]
String filePath = textUri.getEncodedPath();
mDisplay.setText(filePath);
i have tried:-
1] to get the string bytes before passing string via intent
2] getting url and then converting to string
and 1 or 2 more methods. all are displaying the url and not the string, i feel the issue may lie in this line
Uri fileUri = Uri.parse(file.getUrl());
but i am not sure.

Related

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri

The Situation:
I decremented a Uri
First, I converted the Uri into a string and in turn into an int
Afterwhich, I did a -1, and then I got the string that looks exactly like a decremented string
However, when I parse the uri and try to setImageURI() on it,
it is showing "File error accessing recents directory (directory
doesn't exist?)."
Here is the code that I have used:
Uri ImageUri = data.getData();
String uri1 = ImageUri.toString();
//region uri2
String substr1 = uri1.substring(uri1.length()-3);
int substr1int = parseInt(substr1)-1;
String decrementedstr1 = new Integer(substr1int).toString();
int numberofchars1 = uri1.length()-3;
String firstcomponent1 = uri1.substring(0, numberofchars1);
String uri2 = firstcomponent1 + decrementedstr1;
//endregion
Uri test = Uri.parse(uri2);
animateobject.setImageURI(test);
Got this Error:
File error accessing recents directory (directory doesn't exist?).
After I used 'Debug App', it showed the error in more details:
java.lang.SecurityException: Permission Denial: reading
com.android.providers.media.MediaDocumentsProvider uri
content://com.android.providers.media.documents/document/image%3A1000002538
from pid=1309, uid=10925 requires that you obtain access using
ACTION_OPEN_DOCUMENT or related APIs
Note: This is in java and I'm using Android Studio to code.
Let's first get something straight. What is the meaning of that % character?
Well ... if you look at the URI Specification (RFC ....) the % is a percent encoding marker. The two characters after the % are hex digits, and the whole thing represents an ASCII character. In fact, %3A represents the colon character (:). So the unencoded "opaque" component of that URI is actually
//com.android.providers.media.documents/document/image:1000002538
Thus, the image (document) number is really 1000002538 and decrementing it should give 1000002537 as the image number.
I'm not entirely sure why your "string bashing" approach is failing, but you are decrementing just the last 3 digits of the image numbers ... and your example has 4 significant digits on the right end.
So here's how you should code it:
Uri imageUri = data.getData();
String[] pathSegments = imageUri.getSchemeSpecificPart().split("/");
String lastSegment = pathSegments[pathSegmentslength - 1);
String[] parts = lastSegment.split(":");
assert parts.length == 1 && "image".equals(parts[0]);
long imageNo = Long.parseLong(parts[1]);
imageNo--;
lastSegment = "image:" + imageNo;
pathSegments[pathSegments.length - 1] = lastSegment;
String path = String.join("/", pathSegments);
imageUri = Uri.Builder().scheme("content").opaquePart(path).build();
By calling getSchemeSpecificPart() we are getting the relevant part of the URI with the percent encoding decoded. Likewise, the Builder is going to re-apply encoding as required.
CAVEATS
This code is not compiled or tested. I don't have an Android dev platform.
For non-Android folks, this is using the Android Uri class not the Java SE URI class!

Android Studio WebView error: incompatible types: String cannot be converted to Uri

The following is the code I tried to get "id" value from the currently loaded URL in webview
Uri uri = Uri.parse(webView.getUrl());
String server = uri.getAuthority();
String path = uri.getPath();
String protocol = uri.getScheme();
Set<String> args = uri.getQueryParameterNames();
String chapter = uri.getQueryParameter("id");
Which gives no error, but the app crashes on loading!
Original code was
Uri uri = Uri.parse("http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394");

Espresso image picker - CursorIndexOutOfBoundsException error

I'm developing a test with Espresso to test the profile image change function. I added the following lines in the #Before method in the test.
I create an intent with the image Uri, with my file provider, to return ever that my app goes to the gallery to pick an image.
Intent resultData = new Intent();
String filename = "img1.jpg";
String path = "mnt/sdcard/" + filename;
File f = new File(path);
Context context =InstrumentationRegistry.getInstrumentation().getContext();
Uri contentUri = getUriForFile(context, "com.otsuka.ikigai.fileprovider", f);
resultData.setData(contentUri);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK,resultData);
intending(not(isInternal())).respondWith(result);
The code of the activity that changes user image, calls the following method when it receives the intent,(I must not change it).
mProfileImage = CommonBitmapUtils.rotate(this, data.getData());
profileEdited = true;
imgUserPhoto.setImageBitmap(mProfileImage);
And I'm getting the following error:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Caused by this line in the function rotate of the CommonBitmapUtils class:
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
The cursor has 0 rows don't know why.
Solved it by setting the following path. Without file providers and permissions.
I got the path by debugging the app without testing, and copying the value.
resultData.setData(Uri.parse("content://media/external/images/media/142583"));

How to detect path the file(whether playing from internal or URL stream) being played in Android?

I have a programmed a custom exoplayer videoview to play videos from both internal memory and URL's. Now i want to check whether the video that is played presently in Videoview is from URL Stream or Internal storage. How can i check this?
You can use two methods to determine whether it's internal or remote.
Method 1 - Using Uri
Parse the given URI using Uri.parse(...) then use getScheme() to get the scheme. For (internal) files it will be file.
For example:
String file = "file:///path/of/your/file";
String remote = "https://www.example.com/your_video.mp4";
Uri fileUri = Uri.parse(file);
Uri remoteUri = Uri.parse(remote);
String fileScheme = fileUri.getScheme();
String remoteScheme = remoteUri.getScheme();
Log.d("Scheme", fileScheme); // prints file
Log.d("Scheme", remoteScheme); // prints https
Method 2 - Using File
Create a new File instance from the URI and check if it exists.
For example:
String file = "file:///path/of/your/file";
String remote = "https://www.example.com/your_video.mp4";
File fileFile = new File(file);
File remoteFile = new File(remote);
Log.d("File", "" + fileFile.exists()); // prints true if the file exists
Log.d("File", "" + remoteFile.exists()); // prints false always

android Uri to Java URI

I need a quick help on how to convert android Uri to Java URI. My requirement is to capture images and store them in External storage. To pass these images across activities, I decided to use an Arraylist that holds Uris of images and pass on this arraylist as an intent-extra to next activity. But, Arraylist accepts only JavaURI.
String image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),image);
selectedImageA=Uri.fromFile(photo);
imageIndex++;
selectedImageJ= <Looking for code here>;
imagesUriArray.add(imageIndex,selectedImageJ);
Intent i = new Intent(getApplicationContext(),ScanActivity.class);
startActivity(i);
Try to declare:
ArrayList<Uri> imagesUriArray = new ArrayList<Uri>;
If this doesn't work, declare your ArrayList as String list and convert the uri to String. And later, initialize the uri from the Uri string

Categories