find local path to getUnlockedImageUri - java

I am able to show achievement images using the ImageManager from the URI method getUnlockedImageUri but for some reasons, I need to find the local path to the image because I don't want to use the ImageView and I need the actual file path to the image
The URI of Google Play Games achievement looks something like this content://com.google.android.gms.games.background/images/d2bbfba4/61 and I was hoping to be able to convert it to a File object like below:
File myFile = new File(ach.getUnlockedImageUri().getPath());
Log.i(ExConsts.TAG, "myFile.exists() = " + myFile.exists());
// returns false!
But that does not work! any idea why? or what else I should try? or even tell me if it's possible?

A content:// Uri is a clear sign that either
There is no local file
You do not have direct access to the local file
As stated in the getRevealedImageUri() Javadoc:
To retrieve the Image from the Uri, use ImageManager.
You can use ImageLoader.loadImage(OnImageLoadedListener, Uri) to get a Drawable which can be drawn onto a Canvas using Drawable.draw(Canvas).
You can convert a Drawable to a Bitmap using something similar to this answer if you'd like.

Related

Android Can't Get Cached Image From File Path

We have a Cordova app. I am getting an Image passed to me which is saved in the cache.
I need to make a file from a string url:
File pictureFile = new File(fileURL)
However when I try to load that image file it keeps failing (file not found).
A sample of what I am passed is this:
file:///data/data/co.appname.app/cache/tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg
I have tried making the following urls and none work when I create a file
/data/data/co.appname.app/files/tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg
file:/data/data/co.appname.app/files/tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg
I also tried taking just the file name tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg and getting the cached directory directly and that didn't work either
String path = getFilesDir().getAbsolutePath() + tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg
That just ends up with /data/data/co.appname.app/files/tfss-4cb94488-1843-4ad3-8d02-8802008c7186-1685720347.jpg which still comes up null.
The image HAS to exist there though because we are uploading it to Twitter and Cordova is saving the image in the cache.
Can you advise what the issue might be?
I needed to call getCacheDir() not getFilesDir().

cannot display icon from google places

I have been wrestling with this all night . Here's the issue I am using the google places api in my android project and want to use the icons provided in the json output. I have looked at the questions already posed and they all refer to javascript. Essentially in the log file it keeps stating that it can't find the file when I attempt to access this path. I can put the uri in the browse and it show up any ideas?
the uri is http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png and heres the code it gives a warning too and when the marker is added (because its null errors out)
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
AssetFileDescriptor FDescriptor =
//getContentResolver().openFileDescriptor(uri, "r");
getContentResolver().openAssetFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = FDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
FDescriptor.close();
return image;
}
any help would be greatly appreciated
See the documentation for openAssetFileDescriptor. It doesn't handle the http scheme. You will probably either want to download this file manually and then use a method like the above to load a local file or use a library like Picasso, which will download and cache the image for you (and even load it into your ImageView).

How to get absolute path from a variable type Uri

I'm having trouble getting file paths from uri returned by other activities.
I found many solutions on how to convert Uri to absolute file path like this one but its always dependent on type of content such as audio in the given link.
My app provides an activity to send files of any type.. that said I set the intent filter to
mimetype */*
but when I go in file browser and select a image file from from the camera photo folder I get path like /external/images/media/288 but If I pick a music file I get Uri like external/audio/media/1336
but on the other hand if I choose a non media file type (ex.pdf or zip) then I get the absoute path already in the Uri
So as you can see Its not known what path will Uri have it depends on what file i pick for sending with my app.
one possible solution is to make cursor queries for all types of media e.g audio video and image and if they return null then check for Uri.getPath() but I think thats a very bad practice or inefficient way !
Is there any way to get absolute Path from any Uri (given the file exists) without checking for each type of uri?
I'm having trouble getting file paths from uri returned by other activities.
There does not have to be a "file path" associated with a Uri. A Uri points to a source of data, which may or may not be a file, let alone a file which you can access directly.
You can obtain an InputStream on the data associated with a Uri via a call to openInputStream() on a ContentResolver.
Is there any way to get absolute Path from any Uri
Not in any sort of a reliable fashion. Please use the Uri in the way it was intended.

GImage can't find my image

I was trying the acm library and when I tried to use the GImage it can't find my image. I tried putting it everywhere and still it can't find it. Where does GImage look for an image(please be specific) thank you
I was also having the same problem. I used the full-path and it worked, e.g.:
GImage img = new GImage("/Users/abc/java/programming/abc.jpg");
add(img);
Also, I just noticed that if you are using Eclipse, you can keep the image inside bin sub-directory and then you can use filename:
GImage img = new GImage("abc.jpg");
add(img);
For the above to work, your image should be at: /Users/abc/java/programming/bin/abc.jpg"
According to the docs. it specifically consists of the following steps:
Check to see if an image with that name has already been defined. If so, return that image.
Check to see if there is a resource available with that name whose contents can be read as an Image. If so, read the image from the resource file.
Load the image from a file with the specified name, relative to the application directory or the applet code base.

What is the correct path for Toolkit.getImage()?

I need to upload an image file and generate a thumbnail for the uploaded file in my JSF webapplication. The original image is stored on the server in /home/myname/tomcat/webapps/uploads, while the thumbnail is stored in /home/myname/tomcat/webapps/uploads/thumbs. I'm using the thumbnail generator class I copied from philreeve.com.
I have successfully uploaded the file with help from BalusC. But using Toolkit.getImage(), I can't access the image.
I used the uploaded file's absolute path, like so:
inFilename = file.getAbsolutePath();
The relevant code from the thumbnail generator is:
public static String createThumbnail(String inFilename, String outFilename, int largestDimension) {
...
Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
return "Error loading file: \"" + new File(inFilename).getAbsolutePath() + "\"";
}
...
}
Since I am already using the absolute path, I don't understand why it is not working. I have also used the following values for inFilename, but I always get the "Error loading file...".
/home/myname/tomcat/webapps/uploads/filename.ext
/uploads/filename.ext
But I did check the directory, and the image is there. (I uploaded using /home/myname/tomcat/webapps/uploads/filename.ext, and it works.) What is the correct path for the image in that directory? Thank you.
Update
I got the code to work by using:
Image inImage = ImageIO.read(new File(inFilename));
I still don't understand why Toolkit.getImage() does not work though.
Are you sure it's a JPEG file? Use an image viewer to make sure nothing bad happened to the file during upload (or that it was an image to begin with).
Also, use new File(inFilename).exists() to make sure the path is correct. I also suggest to print new File(inFilename).getAbsolutePath() in error messages because relative paths can hurt you.
That said, the rest of the code looks correct.
The problem is that Toolkit.getImage() does not return the image immediately. The issue is well-described in this bug report, a relevant extract of which is here:
This is not a bug. The submitter is not properly using the asynchronous
Image API correctly. He assumes that getImage loads all of the image's bits
into memory. However, it is well documented that the actual loading of
bits does not take place until a call to Component.prepareImage or
Graphics.drawImage. In addition, these two functions return before the
Image is fully loaded. Developers are required to install an ImageObserver
to listen for notification that the Image has been fully loaded. Once they
receive this notification, they can repaint the Image.
I found that the answer to this question works well:
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();

Categories