Cant set the Linear layout background from a path - android

my code is this
ll.setBackground(Drawable.createFromPath(new ImageLoader().fullPath+"/desiredFilename.png"));
ll is the object of my linear layout ,is this the correct method
Bitmap bitmap = BitmapFactory.decodeFile(new ImageLoader().fullPath+"/desiredFilename.png");
Resources res=getResources();
BitmapDrawable bitmapDrawable = new BitmapDrawable(res,bitmap);
ll.setBackground(bitmapDrawable);
i also used this code but dosent work
shows error noSuchMethods

try this way
ll.setBackgroundDrawable(bitmapDrawable);
instead of
ll.setBackground(bitmapDrawable);

Related

Where is this drawable located?

I used the code from this answer, and compiled everything successfully.
However, I cannot find out how to use the drawable that I have just created. Here is my code:
Drawable dr2 = getResources().getDrawable(android.R.drawable.ic_menu_manage);
Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
Drawable f = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap2, 256, 256, true));
private Integer[] menu_icon = {
android.R.drawable.f,
// I can't figure out how to call Drawable f
};
After converting the drawable to the Drawable f, I am unable to find out how to recall the resized drawable. I have tried using all possible locations in my code as to where it may be (/res/drawable and a few other locations). However, I am unable to compile no matter how I try to call Drawable f. Any help would be greatly appreciated!
(And no, I have eight elements in that array, but only copy+pasted one)
It exists only in memory, not anywhere on disk. You can't reference it by id. Instead, you need to call versions of functions that take a Drawable rather than ones that take an int resourceID.

How to convert a Bitmap to a Drawable?

I'm trying to
Drawable d =new BitmapDrawable(bitmap);
not working, I understand that This constructor is deprecated.
advised to use of BitmapDrawable(Resources, Bitmap)
but do not know how to use this constructor
I lay in Resources?
Using context.getResources(). See also http://developer.android.com/reference/android/content/Context.html#getResources()
Following code helped me.
BitmapDrawable bd = new BitmapDrawable(imgview.getResoureces,bitmap);
where imgview is your target ImageView and bitmap is your required Bitmap.

how to set imageview src?

I have an image view and a string src. I want to set the imageview source to the string src that I have, but am unable to do so beacuse the method expects an int:
imgview.setImageResource(int);
Since this method takes an int how can I accomplish my goal of using a string?
Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.
Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html
e.g.:
imageView.setImageResource(R.drawable.myimage);
To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.
If you have image in the form of bitmap then use
imageview.setImageBitmap(bm);
If you have image in the form of drawable then use
imageview.setImageDrawable(drawable);
If you have image in your resource example if image is present in drawable folder then use
imageview.setImageResource(R.drawable.image);
If you have path of image then use
imageview.setImageURI(Uri.parse("pathofimage"));
What you are looking for is probably this:
ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"
int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);
Let me know if this was helpful :)

Android app crashes when I call bitmap.setPixel

I'm tryimng to see if I can creat dynamic images by creating a bitmap and using setPixel. The program crashes when I call setPixel,
Bitmap bm= createBitmap (50,50, Bitmap.Config.RGB_565);
// program crashes here
bm.setPixel(25,25,0xffffff);
// add a test viue
ImageView mImage= new ImageView(this);
mImage.setImageBitmap(bm);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
layout.addView(mImage);
I figure it is something simple, but cannot figure it out.
Ted
Can you paste the error message for more information ?
You're using Bitmap.Config.RGB_565, did you have tried with Bitmap.Config.ARGB_8888 instead ?

Why is part of my bitmap image missing when written to external storage?

I want to send an image of a view to external storage so that it can be printed. The view is a grid made up of a tablelayout, tablerows and textviews. I write the view to the sdcard, then copy it to my HD using the DDMS file-explorer screen. But when I look at it in MS Paint, the image is only partially there.
I have tested this two ways.
When I usied the xml version, the result was a small black square – no detail, no grid, nothing.
Next I created a textview programmatically, with text using settext(“AAA”). The resulting bitmap had the correct color and size, but the text was missing.
Would someone please tell me how to get my view correctly written to external storage so that it looks like it does on the android screen?
//FYI. Here are excerpts from my program:
//Test-1 used the xml version of the grid:
TableLayout tl = (TableLayout) findViewById(R.id.board);
View viewToBeConverted = tl;
//Test-2 used a simple dynamically generated view:
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.PINK);
tv.setTextColor(Color.BLACK);
tv.setText("AAA");
tv.setHeight(40);
tv.setWidth(40);
View viewToBeConverted = tv;
//Both Tests used this code to write to external storage:
try {
Bitmap returnedBitmap = Bitmap.createBitmap(40,40,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
viewToBeConverted.draw(canvas);
String path = Environment.getExternalStorageDirectory() +
File.separator + strPuzSolFilename;
FileOutputStream out = new FileOutputStream(path);
} catch (Exception e) {
e.printStackTrace();
}
The documentation for draw says:
The view must have already done a full layout before this function is called.
Which means that you must call viewToBeConverted.layout(0,0,40,40) before you can call viewToBeConverted.draw(canvas).
EDIT: I will have to search more about Android's drawing before I can understand what is really happening in your example…
However, if you are simply looking for a way to get a Bitmap from a given View, I would suggest looking at the getDrawingCache() method.
For example:
viewToBeConverted.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = viewToBeConverted.getDrawingCache(false);

Categories

Resources