How to get Image - android

I am new in Android and have very small issue, i want to know what code i need to use to get image.
like if we use this to get text :
name = txtname.getText().toString();
So what we use to get Image ?
CODE:
public static final String TAG_NAME = "name";
public static final String TAG_IMAGEURL = "imageurl"
String name, image ;
Intent in = getIntent();
String mname = in.getStringExtra(TAG_NAME);
String mimage = in.getStringExtra(TAG_IMAGEURL);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
final ImageView imgv = (ImageView) findViewById(R.id.image);
final TextView txtn = (TextView) findViewById(R.id.name);
txtn.setText(mname);
imageLoader.DisplayImage(mimage, imgv);
name = txttitle.getText().toString();
image = // here i want to know what i should need to use to get image
// like i am getting text using :::txttitle.getText().toString();

ImageView.getDrawable() will return the Drawable set in an ImageView

if you've used setImageDrawable() to set image to the ImageView then you can use imgv.getDrawable() method to get the image.

Related

content-driven ImageView

I have one ShowActivity with two Textview and one imageView.Texviews getting resources from string.xml and showing that on ShowActivity,I WANNA MAKE IMAGEVIEW LIKE THAT.
How it possible to imageview getting resources from drawble for each subjects_numbers?
This is my textview code :
TextView tv2 = (TextView) findViewById(R.id.textView2);
String stringName_2 = "subject_text_" + String.valueOf(Subject_number);
int resID_2 = getResources().getIdentifier(stringName_2, "string", getPackageName());
tv2.setText(resID_2);
I wanna code like this for my Imageview. I MADE it but it DOESN'T WORK..
ImageView img = (ImageView) findViewById(R.id.imgview_x);
String imageName = "image_"+ String.valueOf(Subject_number);
int resID = getResources().getIdentifier(imageName,"drawable",getPackageName());
img.setImageResource(resID);
CAN ANYONE HELP ME PLEASE
you should do like this:
int id = getResources().getIdentifier("yourpackagename:drawable/" + imageName, null, null);
imageview.setImageResource(id);

Getting Error in android -- the method setImageResource(int) is the type Image View is not applicable for the argument(string)

I am trying to pass image from one activity to another activity using listview item, but I am getting this error line
the method setImageResource(int)
is the type Image View is not applicable for the argument(string) and at this point of line:-
lblImage.setImageResource(Image);
I don't know now what need to write to remove this error please someone support me and write the exact code to be fit on this line, here I am also placing some code for your reference:
SingleMenuItem(Activity to fetch Image with some text data)::
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String title = in.getStringExtra(KEY_TITLE);
String artist = in.getStringExtra(KEY_ARTIST);
String duration = in.getStringExtra(KEY_DURATION);
String Image = in.getStringExtra(KEY_THUMB_URL);
//Bitmap bitmap =(Bitmap) in.getParcelableExtra(KEY_THUMB_URL);
// ImageView image = (ImageView)findViewById(R.id.thumb_url);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
ImageView lblImage = (ImageView) findViewById(R.id.image_label);
//image.setImageBitmap(bitmap);
lblName.setText(title);
lblCost.setText(artist);
lblDesc.setText(duration);
lblImage.setImageResource(Image); //Getting Error at this line only
//the method setImageResource(int) is the type Image View is not
// applicable for the argument(string)
}
}
ActivityCode (to pass image and text data)
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById
(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(
R.id.artist)).getText().toString();
String duration = ((TextView) view.findViewById
(R.id.duration)).getText().toString();
// byte[] array = null;
// Bitmap thumb_url = BitmapFactory.decodeByteArray
(array, 0, array.length);
String Image=((ImageView)view.findViewById
(R.id.list_image)).getImageMatrix().toString();
// Starting new intent
Intent in = new Intent
(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_DURATION, duration);
in.putExtra(KEY_THUMB_URL, Image);
//in.putExtra(KEY_THUMB_URL, thumb_url);
startActivity(in);
}
You're passing a String to setImageResource(), which requires an integer.
Resource IDs are actually integers - when you refer in code to R.drawable.myImage this is referring to an integer resource in automatically generated code (R.java) which is created by the compiler.
To load an image from a resource string (not recommended) see Android - Open resource from #drawable String
This will work for image id not for image string
if you want show image in the imageview do it like this:
lblImage.setImageResource(R.drawable.image);
OR
according to your requirement you have to get Drawable from the imag link. by using this code you can get image drawable and then set to image view
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
call this function and the set drawable to image view like this.
Drawable image = LoadImageFromWebOperations(Image); // Image means iamge url getting
from previous activity
imageview.setImageDrawable(image);

Drawable Images in SQLite android

I've got a working TextView using database populating text, but I want to connect the image path name in my local database. How can this be incorporated in java?
In the main activity I am setting :
String image = currentQ.getImage();
ImageView iView = (ImageView) findViewById(R.id.IS);
Bitmap bMap = BitmapFactory.decodeFile(image);
iView.setImageBitmap(bMap);
In the question:
private String image;
/**
* #return the image
*/
public String getImage() {
return image;
}
/**
* #param image the image to set
*/
public void setImage(String image) {
this.image = image;
}
And in the database I am declaring it as a text:
file.path.name/drawable/i1.jpg and so on for the others.
How can I attach an image file location in my drawable folder for each question in the database and connect it in the java?
Thanks in advance.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
String imageName = "NameofImage"; // store only image name in database(means not store "R.drawable.image") like "image".
int id = getResources().getIdentifier("projectPackageName:drawable/" + imageName, null, null);
imageView.setImageResource(id);

How to add Image Dynamically android

I Am Having Base class which is called common activity,In that i am returning the String in every activity.By using the following code.
txtHeader = (TextView)findViewById(R.id.txtHeaderText);
txtHeader.setText(getScreenHeader());
and i am returning the empty string
protected String getScreenHeader(){
return "";
}
In another activity i will do in this way.
public final static String SCREEN_NAME = "Dashboard";
#Override
protected String getScreenHeader() {
return SCREEN_NAME;
}
How can i do this For IMAGE-VIEW.
Sorry for my English.
Thansks,
Nikhil.
You will need to use a Bitmap object such as this:
Bitmap b = getBitmapImage();
ImageView img = (ImageView )findViewById(R.id.imgView);
img.setImageBitmap(b);

how to get name of drawable of an imageview

I want to get the name of background image of an imageview, but my code is showing the id of the imageview.
ListView lst = list.get(0);
RelativeLayout view = (RelativeLayout) lst.getChildAt(i);
ImageView imgV = (ImageView) view.getChildAt(1);
String checkBoxBtn = imgV.getResources().getResourceEntryName(imgV.getId()).toString();
please help
The name isn't stored. You have to store it in a variable.

Categories

Resources