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).
Related
want get album art of song from url and this is my try so far :
SongPath = "www.asd.com/music.mp3";
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try{
mmr.setDataSource(SongPath);
}catch(Exception e){}
byte [] data = mmr.getEmbeddedPicture();
if(data != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
}
else
{
imageView.setImageResource(R.drawable.jak);
}
but when run this code get this : call to get embedded picture failed
so i research about this and some people fix that with change this part
mmr.setDataSource(SongPath);
to this
mmr.setDataSource(SongPath,new HashMap<String, String>());
i do that but when run app image view show nothing and get this : SkImageDecoder::Factory returned null
Note :
the only way i could do that use FFmpegMediaMetadataRetriever library (its like MediaMetadataRetriever) and worked but problem is library seems slow .. its mean 4,5sec time need to fetch pic and when add this library the apk file from 1.8mb become to 24mb! and this is so huge!
so any one in the world know how can do that with good way ? if any one can please help
You can use Glide in a similar way you would use it for a File: bumptech/glide#699 if you can figure out how to do it remotely. The problem is that you still need to download the whole .mp3 file, which takes that 4-5 seconds I guess. I'm not exactly sure about the .mp3 format, but I think the album art may be at the end of the file. For this reason this approach is not suggested (similar to how it's a bad idea to load video thumbs from http). If you want to go this way anyway, then slap a .diskCacheStrategy(SOURCE) on the load to save the file first, and then write a custom decoder to use
The best approach is to have the album art served from a separate file if you host the mp3; or use a public service to retrieve it. See any album art downloader program for possible services.
One thing's for sure: without a protocol, nothing will work, prefix your www. with http:// or https:// as necessary.
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.
Hello I use Universal Image Loader to load images from the device , now it works, but if the file path contains a "space character" the image does not get displayed and log records show that there is a FileNotFoundException .
I tried to open the file in a thread using java io and it opens and I can read it.
the file name :
/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20121014-WA0001.jp
when the Exception thrown
it replace the space with a %20 and this what makes the exception thrown.
My code:
ImageLoader.getInstance().displayImage(
Uri.fromFile(
new File(cursor.getString(cursor.getColumnIndex(
MediaStore.Images.Media.DATA)))).toString(),
holder.mImage);
works only when no spaces in the path ,
Any help
The other answer unfortunately isn't too clear on what fileName is so after some additional digging I managed to use local image with imageloader using:
Sample Code:
string imgPath = "/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20121014-WA0001.jpg";
String decodedImgUri = Uri.fromFile(new File(imgPath)).toString();
ImageLoader.getInstance().displayImage(decodedImgUri, imageView);
Android loading local image with space in path and with universal image loader also helped to resolve this.
I had the same problem and I found this solution.
String uri = fileName.getUri().toString();
String decodedUri = Uri.decode(uri);
ImageLoader.getInstance().displayImage(decodedUri, imageView);
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();
Android seems to make life pretty easy for loading resources of certain types. Once I leave the beaten path a little bit, it seems less helpful. I can load in images, sounds and other objects from the assets folder if and only if they are of certain types. If I try to load a binary file of my own format, I get a less than helpful 'file not found' exception, even though listing the directory shows that it is clearly there.
I've tried using the following methods to read a binary file of a custom format from the assets directory:
File jfile = new File("file://android_asset/"+filename); //tried to get the URI of the assets folder
JarFile file = new JarFile("assets/"+filename); //tried assuming the assets folder is root
fd = am.openNonAssetFd( filename); //tried getting my file as an non asset in the assets folder (n.b. it is definitely there)
fs = am.open(filename, AssetManager.ACCESS_BUFFER); //tried loading it as an asset
I'm thinking that there's something fundamental about android file I/O that I don't understand yet. The documentation for asset management seems incomplete and there must be some reason for deliberately making this unintuitive (something to do with security?). So, what's the fool proof, canonical way of loading a binary file of my own format within an android app?
UPDATE:
I tried file:///android_asset/ but still no joy.
String fullfilename = "file:///android_asset/"+filename;
File jfile = new File(fullfilename);
if (jfile.exists())
{
return new FileInputStream(jfile);
}
else
{
return null; //the file does exist but it always says it doesn't.
}
Are there any permissions for the file or in the project manifest that I need?
Thanks
I think the best way to load a file from the Assets folder would be to use AssetManager.open(String filename) - this gives you back an InputStream which you can then wrap in a BufferedInputStream and otherwise call read() to get the bytes. This would work regardless of the file type. What kind of problems have you had with this approach specifically?
I think you have left out the slash as in
File jfile = new File("file:///android_asset/"+filename);
There's three forward slashes, not two. :)
For me the solution was to uninistall the application, clean the project in Eclipse and run it again. The problem was Android couldn't find the new files I put in the asset folder.
I ended up reading this question so I hope this can be helpful to someone else.