Loading drawable from sd card - java

I'm trying to load a png image as a drawable from my device sd card.
I use the following function but it doesn't work:
public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}
The image path is: mnt/sdcard/MyFolder/image.png
The app crashes when I try calling that method, how should I load my png image located in my sdcard and cast it into a Drawable object?

There is actually a BitmapDrawable constructor straight from file path. The method you are using is depricated. Try:
Drawable myDrawable = new BitmapDrawable(getResources(), pathName);
If this doesnt work, Try getting a bitmap and creating a drawable from it:
The bitmap can be created with decodeFile
You can use it like this:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Then you can use the bitmap for drawing etc.
to convert Bitmap to drawable use
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info.

I am simple do like that
public Drawable getDrawableFromPath(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
//Here you can make logic for decode bitmap for ignore oom error.
return new BitmapDrawable(bitmap);
}

Related

Set background in linear layout from Bitmap from file

I would like to set background in LinearLayout from file, path is correct, but not works
Bitmap bitmap5 = BitmapFactory.decodeFile(data.getData().getPath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap5);
mDrawingPad.setBackground(bitmapDrawable);

How to convert url to image to Image?

I get url to my Image, and want it make Image;
I want use this methods
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
or
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
Image image = ImageIO.read(url);
but Android Studio can't find ImageIO and java.awt.Toolkit. How can I add them?
You can get Bitmap from url and then you can use that bitmap further to set image of an ImageView.
URL url = new URL("http://....");
Bitmap image=BitmapFactory.decodeStream(url.openConnection().getInputStream());

Android: ImageView and SD-card

I am trying to read an image from /mnt/sdcard/img.jpg into ImageView.
Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/img.jpg");
imageView1.setImageBitmap(bm);
I have write external storage permission.
But ImageView is empty, LogCat don't get any errors,
How can I fix it?
Try
File file = new File("/mnt/sdcard/img.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview1.setImageBitmap(bitmap);

Use Current Wallpaper as live wallpaper background

I am getting the current wallpaper by using following code:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
How can I create a bitmap from this?
like when I create a bitmap from res folder I use this
Resources res = getApplicationContext().getResources();
b = BitmapFactory.decodeResource(res, R.drawable.wall);
What code I should use to get current wallpaper into the bitmap so I can draw it on my canvas and use it as my live wallpaper background?
The Drawable fetched should really be a BitmapDrawable. You can verify this using instanceof if necessary.
That being the case, all you have to do is:
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final Bitmap wallpaperBitmap = ((BitmapDrawable) wallpaperDrawable).getBitmap();
EDIT: If it turns out that the Drawable is NOT a BitmapDrawable, you can use the following method to convert it (found in this answer, credit to André):
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;
}

Initializing Bitmap variable with bitmap drawable from resources is not working - using ANDROID

I use the following code to initialize my bitmap variable:
Bitmap bitmap = ((BitmapDrawable)this.model.gameView.context.getResources().getDrawable(R.drawable.pic1)).getBitmap();
When I try to log the width of that bitmap, the log does not even output anything for that call.
I know it's making it to that line, because I traced the code.
Also, when I try to do canvas.draw for the bitmap, nothing is drawn on the screen.
Everything I draw with rectangles works fine.
My picture resource is a PNG.
Any help is appreciated!
Try something like this for your bitmap class.
public class DrawBitmap extends View
{
Bitmap bitmap;
public DrawBitmap(Context content)
{
super(content);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);//whatever color you want, make sure it's not the same as your image
canvas.drawBitmap(bitmap, (canvas.getWidth()), 0, null);
}
}
Main Class
public class Main extends Activity
{
DrawBitmap myView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myView = new DrawBitmap(this);
setContentView(myView);
}
}
Try using BitmapFactory.decodeResource
Have a look at the answer in this topic:
How to convert a Drawable to a Bitmap?
Just figured it out. It had nothing to do with the method of bitmap loading I used.
It was a logical error on my part. My code accidentally reached a case where my bitmap became null, and it tried to draw the null resource on the canvas.

Categories

Resources