Not able to set Bitmap to wallpaper using wallpaperManager - android

I have a string which consists an URL of the image, in the same activity I am viewing the image through URL. But to set the same image as my wallpaper, I am converting the string to Uri and then to Bitmap to use setBitmap.But I am still getting error telling No image was chosen.
Code is below:
newString has the URL of the image.
final String myUrlStr = newString;
URL url;
Uri uri=null;
try {
url = new URL(myUrlStr);
uri = Uri.parse( url.toURI().toString() );
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
} catch (IOException e) {
e.printStackTrace();
}
setButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
// Set the image as wallpaper
if(image!=null)
wallpaperManager.setBitmap(image);
else
Toast.makeText(getApplicationContext(), "No image was chosen.", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

It seems like I can't comment. So i answer here.
I am not sure about your url being on internet or local. I don't find anything wrong with your code. So, my deduction is your onclicklistener is set before it can fetch image.(might need to use asyntask to save image) Are you displaying on your imageview from same image resource?

Related

Loading pictures from AWS S3 takes too much time

In my Android app, I have a GridView which uses a BaseAdapter to load data from DynamoDB and S3 in order to set every cell.
This is the code which I'm using to get the items pictures from S3:
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Bitmap> future = exec.submit(new Callable<Bitmap>() {
#Override
public Bitmap call() {
try {
url = new URL(/*S3 url*/);
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
Log.e("Malformed", e.getMessage());
} catch (IOException e) {
Log.e("IO", e.getMessage());
}
return bmp;
}
});
try {
holder.itemImage.setImageBitmap(future.get());
} catch (ExecutionException e) {
Log.e("Execution", e.getMessage());
} catch (InterruptedException er) {
Log.e("Interrupted", er.getMessage());
}
The code gets a Bitmap from S3 URL, and sets the ImageView on the GridView cell layout as the .jpg file on S3 URL. The problem is that getting the images from S3 takes very long.
Is there any way to speed this process? Maybe writing this code at the beginning and load it there?

Converting a String to an Integer in Android

I'm trying to use WallpaperManager in a ViewPager. I've got a button which is supposed to set the current image in the ViewPager to wallpaper. My problem comes with the line of code wallpManager.setResource(newInt);... the integer it comes up with is always 0 (zero), and so the app crashes and LogCat says there's no Resource at ID #0x0. As a test to see if I'm getting the correct image URL I've done this:
String newStr = images[position];
CharSequence cs = newStr;
Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();
And the resulting Toast shows the correct image URL. I can't figure out how to convert the URL which is in the form of "http://www.example.com/image.jpg" to an Integer so that the WallpaperManager can use it. Here's what the whole button code looks like:
wallp_BTN.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());
String newStr = images[position];
int newInt = 0;
try{
newInt = Integer.parseInt(newStr);
} catch(NumberFormatException nfe) {
}
CharSequence cs = newStr;
try {
wallpManager.setResource(newInt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();
}
});
Set wallpaper from URL
try {
URL url = new URL("http://developer.android.com/assets/images/dac_logo.png");
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
wallpaperManager.setBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Enjoy =)
A method wallpaperManager.setResource() requires resource id from your application. Example: I've ImageView with id "myImage" then call the method will look like as wallpaperManager.setResource(R.id.myImage).
In your case your id not valid.
Instead of wallpManager.setResource(0) you should use the wallpManager.setResource(R.drawable.yourimage) since it's expecting a drawable and there is none in your app with id = 0.
In your code
String newStr = images[position];
int newInt = 0;
try{
newInt = Integer.parseInt(newStr);
} catch(NumberFormatException nfe) {
}
Since newStr is never a number, always a url so NumberFormatException is caught always. Hence the value of newInt is always initialized to 0. Hence the error.
I realized that I have to download the image before I can set it as wallpaper! Thanks for your help AndroidWarrior and Owl. When I get the download code worked out, I'll post it. Owl showed me how to download the wallpaper:
//--- Wallpaper button
wallp_BTN.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
vpURL = new URL(images[position]);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap = BitmapFactory.decodeStream(vpURL.openStream());
wallpManager.setBitmap(bitmap);
Toast.makeText(UILPager.this, "The wallpaper has been set!", Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
//--- END Wallpaper button
... and I also figured out how to download the ViewPager image as well (I need to do both in different areas of my app):
//--- Wallpaper button
wallp_BTN.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String vpURLStr = images[position];
GetVPImageTask downloadVPImageTask = new GetVPImageTask();
downloadVPImageTask.execute(new String[] { vpURLStr });
}
});
//--- END Wallpaper button
//--- Download ViewPager Image AsyncTask
private class GetVPImageTask extends AsyncTask<String, Void, Bitmap> {
ProgressDialog getVPImageDia;
#Override
protected void onPreExecute() {
super.onPreExecute();
getVPImageDia = new ProgressDialog(UILNPPager.this);
getVPImageDia.setMessage("Grabbing the image...");
getVPImageDia.setIndeterminate(false);
getVPImageDia.show();
}
#Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
#Override
protected void onPostExecute(Bitmap result) {
try {
getVPImageDia.dismiss();
getVPImageDia = null;
} catch (Exception e) {
// nothing
}
Toast.makeText(UILNPPager.this, "The image be Downloaded!", Toast.LENGTH_SHORT).show();
//downloaded_iv.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
//--- END Download ViewPager Image AsyncTask

Display Photo in Android App from Server

There is no issue if I display only Image on any Activity as I will convert Image to Bitmap in doInBackGround().
But what If I am using Custom ListView and I want to display Image on each Item ?
I am using adapter.setViewBinder for this. But issue is that I can't use it in doInBackground() so I need to use in onPostExecute() and this is the big problem. Now, if there are more images then app will freeze for sometime till all images convert into Bitmap.
How to prevent this issue and complete all process in doInBackground() only ?
My Code :
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.getId() == R.id.imgHotelPhoto) {
String Photo = data.toString();
try {
url_Hotelhoto = new URL("" + WebsiteURL"/images/HotelSmallPhoto/" + Photo + "");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
bmp_HotelPhoto = BitmapFactory.decodeStream(url_Hotelhoto.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((ImageView) view).setImageBitmap(bmp_HotelPhoto);
return true;
}
return false;
}
});

how to set background image to PDF page using iText?

I am using iText for creating pdf, i need to set background image to first pdf page but image is high resolution, how can set background image with out reduce image quality. Please help me.
there is work around for doing this by setting the image at absolute position and setting the page size equal to background image and don't forget to put the image in proper dpi folder :)
private void setBackground(Document document) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.background_img);
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, stream);
Image img;
try {
img = Image.getInstance(stream.toByteArray());
img.setAbsolutePosition(0, 0);
document.add(img);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Setting the Wallpaper from an Image on the SDCard

How would one go about setting the homescreen wallpaper from an image on the SDcard?
i.e.:
try {
wallpaperManager.setResource("/sdcard/wallpaper/olive.jpg");
finish();
} catch(IOException e) {
e.printStackTrace();
}
hasn't worked, returned an error: 'The method setResource(int) in the type WallpaperManager is not applicable for the arguments (String)'
Bitmap o = BitmapFactory.decodeFile("/sdcard/wallpapers/olive.jpg");
try {
wallpaperManager.setBitmap(o);
finish();
}
catch (IOException e) {
e.printStackTrace();
}

Categories

Resources