Setting background image found in sd card - android

In my application I want to be able to allow users to place in images onto a folder created by my app which then I put them into a stack which will be set up as an image background.
The question is how would I be able to set a background image on code?
I know you can place images in drawables and set it on xml but I want users to be able to feel free to choose from the background they prefer.
Thanks.
This is what I tried. If possible correct me cause it's not working.
Bitmap backgroundBitmap = BitmapFactory.decodeFile(backgroundFileLocation);
Drawable backgroundImage = new BitmapDrawable(backgroundBitmap);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout_layout_options);
ll.setBackgroundDrawable(backgroundImage);

use a linear layout and setBackgroundDrawable() once you get a drawable from the sd card by loading the image using bitmapfactory.decodefile() then use bitmapDrawable

Related

Hook to get image after being loaded from a layout

Is there any way to get an image being pointed by an android layout and modify it before it is receceived by the requester?
For example if I have an image view like:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage.png"
/>
Is it possible to register to any hook (or any other way) where I could modify the image before the ImageView receives it?.
Trying to clarify a bit more the question:
1) The ussual way android works:
The app runs, and reads a layout with an imageview requesting #drawable/myimage.png
The image is loaded and provided to the imageview to show it
2) What I would like to try or accomplish:
The app runs, and reads a layout with an imageview requesting #drawable/myimage.png
I previously, somehow, told android to send me the image/stream before
providing it to the imageview. I modify it
I return the modified image and it is shown in the imageview.
Do anybody know if this is possilbe? I have seen offuscators like dexguard that can do this. Any tip would be really helpful because I haven't found any way to do it after some days of investigation.
Cheers.
You will need to extract bitmap from resources
Modify the bitmap
Apply modified bitmap to imageview through java code
These snippets might help
//extract bitmap from drawable resources
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon_resource);
//modify bitmap code here
//apply modified bitmap to image view
imageview.setBitmap(icon);

How to get the name of drawable through resource id?

I am trying to make the bitmap and for this I need the drawable name such as:
overlayScaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.monkey);
This is working fine But my problem is that I can not do this same thing when a user selects a picture from a list because I have done this statically. As you can see I have wrote R.drawable.monkey. So its means every time it is going to create bitmap with monkey image.
But it is not the case, the user selects image which gets fixed into the image view. Now I want to get the ImageView as drawable. If it is possible then it could be dynamically and would be easy to handle. Any idea How I can get drawable of ImageView to use in bitmap scaling? please help
You could create an enum object containing all refs to the bitmaps and an id.
But another way is using getTag() and setTag() on the resources to identify by.

Trying to use transparent image but a black background is being added

I have a transparent image in my drawable folder and I'm trying to send it using an Intent. The image loads and sends just fine but it's no longer transparent and has a black bacground attached to it. This is how I'm sending the image
Intent localIntent = new Intent("android.intent.action.SEND");
localIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("android.resource://com.t4t.stickerwhatsapp/" + videoDetails.get(pos)));
localIntent.setType("image/*");
startActivityForResult(localIntent,111);
videoDetails.get(pos) is the id of the image in the drawable folder. Why is it adding a black background to my images?
Edit: It turns out my phone is adding the black bg to all transparent images that I receive it's not the code adding it before it sends.
You should get the drawable out of the resources by using the resources available from getResources().
I recommend you use:
Drawable drawable = getResources().getDrawable(R.drawable.image);
The id of an image may change and keeping tabs on it is a bad idea. If you must do this, instead you may use:
getResources().getIdentifier("image", "drawable","package_name");
If the image is still not transparent, I might try setting the drawables background color using Color.Transparent. It can be a number of issues, especially if on the other end the drawable is getting processed.
It turns out my phone is adding the black bg to all transparent images that I receive it's not the code adding it before it sends.

downloaded png image with transparent background shows with black background

In my android application I downloaded some png image and write them in sd card. when I show them in image view they are showing with black background. the original images have transparent background. How I can keep the background transparent. Please help me. I use PNG format to compress and write it to sd card
Set The View Background as
R.color.transparent
or in xml like
android:background="#android:color/transparent"
Edited :
Thanks for reply. yes I did this. I did just now One more thing, I set canvas color android.R.color.transparent and it worked

how to display images from a selected folder in the form of a slideshow

I am using the following code to pick a folder from the SDCard.
Environment.getExternalStorageDirectory();
After selecting the folder, I return the path of the folder and display it in a text view currently.
What I want to do is, I want to display all images in the selected folder in the form of a slide show. How do I go about in doing this?
1. convert images in the Bitmap.
Bitmap bm = BitmapFactory.decodeFile(String pathName);
Decode a file path into a bitmap.BitmapFactory
2. Using ImageView set that Bitmap in ImageView.
ImageView.setImageBitmap(Bitmap bm);
Sets a Bitmap as the content of this ImageView.
3. For slide show just after some delay (use timer) after change the bitmap of ImageView.
We are appreciate If you are do by yourself. Without finding any code.
EDIT: Here Mihai Fonoage's Blog Displaying Images from SD Card In Android - Part 2 It display images from sdcard in Gridview. You can modified it and display Images one-by-one as a slideshow.
If all you want to do is cycle through the images one by one, there are numerous options. You could for example simply use a Timer (or preferably a ScheduledThreadPoolExecutor if you're writing production code) with a fixed interval or have a Handler repeatedly post itself with a certain delay. With each 'tick' you can then simply set the next image.
If you're after something a little more fancy, it may be worth looking at implementing an ImageSwitcher, which provides the ability to also show thumbs of upcoming/previous images. Code examples are wide spread, e.g. here (scroll down a bit).

Categories

Resources