I have found plenty of details on how to set a wallpaper from a drawable etc however is it possible to do this from a file location.
I have confirmed that the following code prints out the location of the file
Toast.makeText(MyWallpapers.this, "" + listFile[position].getAbsolutePath(), Toast.LENGTH_SHORT).show();
String ImageLocation = listFile[position].getAbsolutePath();
And so I have been trying something like this to get it to set the wallpaper.
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(ImageLocation);
} catch (IOException e) {
e.printStackTrace();
}
But it doesn't like it.
Any advice?
Load the file into a Bitmap using BitmapFactory and call WallpaperManager.setBitmap
Related
I'm trying to make an application to manage some drink recipes...
I need to show the drink image in a JPanel already working with the file path like this:
ImageIcon image = new ImageIcon("src/fotos/trinidad.jpg");
The problem is that when I try to set this path setting it with the object name, the image is not being loaded.
String s = ("src/fotos/"+b.getNome().toLowerCase()+".jpg");
ImageIcon image = new ImageIcon(s);
Printing this string s I have this result:
System.out.println(s);
src/fotos/trinidad.jpg
Apparently it looks the same path, but the image is not being loaded.
What am I doing wrong?
Try something like this, if the image (path) does not exist you can catch the exception and treat accordingly.
BufferedImage drinkImage = null;
try {
drinkImage= ImageIO.read(new File("src/fotos/trinidad.jpg"));
} catch (IOException e) {
// TODO
e.printStackTrace();
}
ImageIcon image = new ImageIcon(drinkImage);
// Put image in your JPanel
I'm developing an Android app which allows users to upload/download images to and from a database (powered by Amazon AWS). The problem I'm facing is that I can successfully download the files to a directory
/storage/emulated/0/data/myfile.jpg
But I cannot display them as a new ImageView.
Here are my methods that deal with displaying the methods. Note that RefreshFeedTask.downloadedFiles is a List of Bitmaps as shown here:
do {
objectListing = s3Client.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary :
objectListing.getObjectSummaries()) {
keys.add(objectSummary.getKey());
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
Iterator<String> o = keys.iterator();
while(o.hasNext())
{
String n = o.next();
File file = new File(Environment.getExternalStorageDirectory()+"/data/", n);
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
TransferObserver observer = transferUtility.download(
existingBucketName,
n,
file);
Bitmap m = BitmapFactory.decodeFile(file.getAbsolutePath());
private void refreshFeed()
{
new RefreshFeedTask(this).start();
for(Bitmap f : RefreshFeedTask.downloadedFiles)
{
displayImage(f);
}
}
private void displayImage(Bitmap f){
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(f);
myImage.setVisibility(View.VISIBLE);
Log.i("Background","Displaying file");
}
Any help is appreciated, as I am somewhat new to Android development, but not Java development.
Looks like you have not added you new ImageView to any view, try adding your new ImageView to any container to display it.
parentLayout.addView(theNewImageView);
Another tip: You can try Glide if you want to display many images more efficiently.
One issue that you have here is Ownership.
Advise: That file you just downloaded to that location, your app may not own that location. If this is a closed ecosystem where you are the sole user and owner of that file, you should probably create an application specific directory upon running the application.
The best way to display something that you do not own in the Sdcard is to use the MediaStore API in Android to access them. You need a URI to the right path and just doing an absolute path is not generally advised, especially for image assets.
A good example of this is here:
https://dzone.com/articles/displaying-images-sd-card
Hey Guys I did a little App, where I type into a textbox a specific value (height, weight) and save it into a file.
I did this but I do not know, which path I have to use for Android.
Hope you can help :)
public void SaveList(View view) {
//Pf`enter code here`ad, im privaten Speicherbereich
File file = new File("I need this path :)");
try {
OutputStreamWriter fdg = new OutputStreamWriter(new FileOutputStream(file));
fdg.write(""+this.weight);
fdg.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use Environment.getDataDirectory() to get the root directory, if you dont have an SD card.
If you have an SD card, use Environment.getExternalStorageState()
Read more about them in the docs
Thus, change your code as follows
File file = new File(Environment.getDataDirectory()+"/your_folder_name/your_file_name");
This will create a file with name your_file_name in the folder your_folder_name in your internal storage.
I want to fill an ImageView with an image saved on my localhost. I can display it in a browser fine but it doesn't get displayed in my ImageView.
PHP script to display image:
<?php
include('connect_db.php');
$id = $_GET['id'];
$path = 'Profile_Images/'.$id.'.jpg';
echo '<img src='.$path.' border=0>';
?>
Here is my android code to download an image from a URL:
URL url;
try {
url = new URL("http://192.168.1.13/get_profile_image.php?id=145");
new DownloadImage(this).execute(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void imageDownloaded(final Bitmap downloadedImage) {
runOnUiThread(new Runnable() {
public void run() {
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(downloadedImage);
}
});
}
When I put the URL to some other image it works fine but mine never gets loaded, any ideas?
Also, the following code works but I have a feeling its bad practice..
URL url;
try {
url = new URL("http://192.168.1.13/Profile_Images/145.jpg");
new DownloadImage(this).execute(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Where 145 would be a variable.
Edit
Any reasons for the down-votes would be appreciated!
It's pretty simple, actually. When you send a request to http://192.168.1.13/get_profile_image.php?id=145 a string (<img src=Profile_Images/'.$id.'.jpg border=0>) is sent back. Because the DownloadImage class doesn't parse HTML (it wants raw image data) it doesn't know where the actual image is. Two solutions:
Your 'bad practice' approach
Use this PHP script instead to echo the raw image data:
PHP:
$id = $_GET['id'];
$path = 'Profile_Images/'.$id.'.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($path));
readfile($path);
EDIT: forgot to credit someone: the above code is taken from here (with adapted variable names): https://stackoverflow.com/a/1851856/1087848
Your PHP code will print out something along the lines of:
<img src=Profile_Images/145.jpg border=0>
Not only is this malformed HTML, but it is a text output. Your other URL, http://192.168.1.13/Profile_Images/145.jpg points to an image file, from which the data your receive is an image, not an HTML string.
You should consider having your PHP return a JSON response with the URL of the image ID, and then running DownloadImage on that URL. The advantage this has over a raw echo is that you can easily expand the solution to return other types of files, and even return an array of files.
I am using BitmapFactory.decodeFile to load Bitmaps of images into my application. However, the function returns null on large images (such as those from the camera). The filepath is definitely correct, I just can't figure out why it would return null. I tried supersampling, but it didn't seem to help.
Does anyone have any idea why it would do this or how I could more easily load images taken from the camera into a Bitmap?
Here's the code I am using:
public static Bitmap loadBitmap(String filePath){
Bitmap result = BitmapFactory.decodeFile(filePath);
if(result == null){
if(filePath.contains(".jpg") || filePath.contains(".png")){
//This is the error that occurs when I attempt to load an image from the Camera DCIM folder or a large png I imported from my computer.
Utils.Toast("Could not load file -- too big?");
} else {
Utils.Toast("Could not load file -- image file type is not supported");
}
}
return result;
}
You will need to provide more info about your problem, such as a snippet of code that you are using. If you want to know when/why the BitmapFactory.decodeFile method would return null, you can read directly its source code: http://casidiablo.in/BitmapFactory
For example, one of the reasons that causes BitmapFactory.decodeFile to return null is if there's a problem while openning the file. Curiously, the developers dont't log anything with such a problem... look at the comment "do nothing. If the exception happened on open, bm will be null."
public static Bitmap decodeFile(String pathName, Options opts) {
Bitmap bm = null;
InputStream stream = null;
try {
stream = new FileInputStream(pathName);
bm = decodeStream(stream, null, opts);
} catch (Exception e) {
/* do nothing.
If the exception happened on open, bm will be null.
*/
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// do nothing here
}
}
}
return bm;
}
As you can see, the BitmapFactory.decodeFile does not work standalone... but it uses some other methods of the BitmapFactory class (for instance, BitmapFactory.decodeStream, BitmapFactory.nativeDecodeStream, BitmapFactory.finishDecode, etc.). The problem could be on one of those methods, so if I were you, I would try to read and understand how they work so that I could know in which cases they return null.
Maybe inSampleSize option can help you?
Strange out of memory issue while loading an image to a Bitmap object
It may sound obvious, but check that your filePath actually points to a file. You mention that you are using a file manager to select the image to open - it's possible that the file manager is returning a path to a content provider instead of a file.
There is a more robust way to open files using the ContentResolver class, which can open an InputStream to a content provider, file, or resource, without you needing to know in advance what kind of path you are passing it.
The only catch is that you need to pass a Uri object on the call to openInputStream() instead of a String.
public static Bitmap loadBitmap(String filePath, Context c) {
InputStream inStream;
try {
inStream = c.getContentResolver().openInputStream( Uri.parse(filePath) );
} catch (FileNotFoundException e) {
// handle file not found
}
return BitmapFactory.decodeStream(inStream);
}
This also happens to be how the ImageView widget attempts to load images when you use its setImageURI method.