android change background of activity - android

I am trying to use the following to change the background to an image stored on the sd card:
String pathName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/newbackground.jpg";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.fred);
view.setBackgroundDrawable(bd);
But it keeps crashing.
I´m using setBackgroundDrawable as I want it to work below api 16 and it is deprecated.
I put in a check to see if pathname exists and it does.
Any ideas?
mark

Related

Android Development - How to convert LayerDrawable to Drawable or Bitmap

I'm coding a simple app which takes two images 1)Photo and 2)Filter(Image file) and then Filter will be applied on the photo. But I also want to save the Image in internal storage after the images are overlapped. As you can see in below code, in last second line I created Bitmap Object filteredPhoto so it can be used to save the image but it can't be referenced because of In-convertible types.
There are two ways,
1) Convert LayerDrawable Object to Drawable and later reference it to Bitmap Object (by casting)
2) Convert LayerDrawable directly to Bitmap if possible.
Can anyone explain me how to make this happen?
Note : This question may have been asked by others but none of them has replied them properly. Most of them said use Canvas but I can't find how Canvas can help me in my problem so kindly don't mark my question as Duplicate.
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theImageViewMID = (ImageView) findViewById(R.id.theImageView_MID);
Drawable layers[] = new Drawable[2];
layers[0] = ContextCompat.getDrawable(this, R.drawable.image_1546);
layers[1] = ContextCompat.getDrawable(this, R.drawable.new_filter);
layers[1].setAlpha(75);
LayerDrawable layerDrawable = new LayerDrawable(layers);
theImageViewMID.setImageDrawable(layerDrawable);
layerDrawable.set
Bitmap filteredPhoto = ((BitmapDrawable) LayerDrawable).getBitmap();
//MediaStore.Images.Media.insertImage(getContentResolver(), filteredPhoto, "THIS IS TITLE", "THIS IS DESCRIPTION");
}

I went upto here to add an image to my android app but there is an error line

I went upto here to add an image to my android app but there is an error line:
String pathName = "chemistry\2013_03_26\chemistry1.pdf";//Error line
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
I think you should use absolute path with getFilesDir() or getExternalFileDir().
You can find more information at clickHere

Android - setBackgroundResource on Button

I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);

Linearlayout background image in assets folder

I want to use a Image from the assets folder as my background image in android
but the only picture I can use is out oif the drawable folder
random = String.valueOf(util_random.random(1, 4));
String drawable = "/assets/images/img"+random ;
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(drawable);
this.setContentView(ll);
You cannot pass a String in View.setBackgroundDrawable(Drawable).
Use BitmapFactory to create a Bitmap, then create a BitmapDrawable and use it as your background image.

Copy Bitmap contents of one ImageView to anoher

This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.
Here is some code that doesn't work.
ImageView ivSrc = (ImageView) findViewById(R.id.photo);
ivSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException
Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null
View vSrc = (View) ivSrc.getParent();
vSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap
//To test the bitmaps:
ImageView ivDest = (ImageView) findViewById(R.id.photo2);
ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above
I have to going about this wrong because doing a copy should be so easy. TIA
Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?
The way I'd do it:
Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);
Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.

Categories

Resources