I want to set the image res/drawable-hdpi/nasa_image.jpg
as the wallpaper.
I wrote the following code but it raises the FileNotFoundException
.
Uri u1 = Uri.fromFile(new File("res/drawable-hdpi/nasa_image.jpg"));
Uri u2 = Uri.parse("android.resource://com.my.package/drawable/nasa_image.jpg");
Uri u3 = Uri.parse("android.resource://com.my.package/" + R.drawable.nasa_image);
WallpaperManager w = WallpaperManager.getInstance(this);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(u));
w.setBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
First I tried for u1
. It didn't work. Then I searched and found this. I then tried u2
and u3
.
I checked the log. Every time it gave the same FileNotFoundException
.
How to refer to it? Where am I wrong? Am I taking the wrong root directory?