1

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?

Community
  • 1
  • 1
Shashwat
  • 2,538
  • 7
  • 37
  • 56

3 Answers3

0

I think this is a wrong way to do that in android. why don't you use the resource identifier?

ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(nasa_image, "drawable", getPackageName());
Drawable drawable=res.getDrawable(id);
imgView.setImageDrawable(drawable);

or

ImageView imgView=(ImageView)findViewById(R.id.imageView1);
Drawable drawable= getResources().getDrawable(R.drawable.nasa_image);
imgView.setImageDrawable(drawable);
imgView.setImageDrawable();

Update to convert from drawable to bitmap just use this (assuming that your drawable is an instanceof Bitmapdrawable)

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

Use this method if you are not sure about drawable instance

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

Use this method if you are sure that you the right file path

 private Drawable getDrawable(String path) {
      File imgFile = new File(path);
      if (imgFile.exists()) {
         Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
         return new BitmapDrawable(resources, bitmap);
      }
      return null;
   }

you can adjust it to return bitmap or drawable according to your issue

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • This is fine but I need `Bitmap` object to use `w.setBitmap` method. – Shashwat Mar 09 '13 at 13:23
  • @Shashwat simple just use this Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); to convert from drawable to bitmap – Festus Tamakloe Mar 09 '13 at 13:29
  • I have just found a method to set the wallpaper `w.setResource(R.drawable.nasa_image)`. But still thank you for suggesting it. Is there any way by which we can refer the file using the filename? – Shashwat Mar 09 '13 at 13:41
  • I didn't find any method `res.getDrawable`. To convert from resource id to `Drawable` we can do ` Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);`. You can make your answer clear. – Shashwat Mar 09 '13 at 13:50
  • I you know the absolut path you can also get the bitmap or drawable from a file. look at my update @Shashwat – Festus Tamakloe Mar 09 '13 at 13:54
  • Thats what I was struggling for in the question. What should be the string for the path? – Shashwat Mar 09 '13 at 14:22
  • use this file:///android_asset/..., where ... is the stuff inside your assets/ @Shashwat – Festus Tamakloe Mar 09 '13 at 15:17
0

You should not include the extention (.jpg) Use:

Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/drawable/nasa_image");

Roel
  • 3,089
  • 2
  • 30
  • 34
-1

Try

Uri u1 = Uri.parse("R.drawable.nasa_image");
Connor Tumbleson
  • 3,270
  • 2
  • 25
  • 37