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);
Related
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.
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);
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);
In my project I'm trying to display several image files by manipulating the filename of one of the images programatically.
ie, I may have:
filename.jpg, filename_top.jpg, filename_middle.jpg
I receive input of an drawable int and am trying to find the filename of the displayed image before manipulating this filename and trying to display the programatically generated filenames.. problem is that the manipulated filename does not display.
ie. there is something wrong with this:
imageView2.setImageResource(getImageId(this, namebottom));
Any ideas how getImageId can be modified to make setImageResource work properly?
The code would look something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bun = getIntent().getExtras();
int imagenumber = bun.getInt("imagenumber");
String extension = bun.getString("extension");
// int become a val from 0 to 20 (array size)
setContentView(R.layout.clickeditem);
final int[] imgIds = new int[{
R.drawable.image0,R.drawable.image1,R.drawable.image2,,,R.drawable.image20};
//The first image with id top in the layout is set ok:
ImageView imageView1 = (ImageView)findViewById(R.id.top);
imageView1.setImageResource(imgIds [ imagenumber ] );
// problem here:
//try to get the name of this file: ie: filename.jpg
// and then manipulate the filename:
String name = imageView1.getResources().getString(R.id.image0);
//try to convert this to the filename_middle.jpg
String namemiddle = name.replace(".jpg", "_middle.jpg");
imageViewt.setImageResource(getImageId(this, namemiddle));
//try to convert this to filename_bottom.jpg
String namebottom = name.replace(".jpg", "_bottom.jpg");
imageView2.setImageResource(getImageId(this, namebottom));
}
//where getImageId is defines as follows:
public static int getImageId(Context context, String imageName)
{
return context.getResources().getIdentifier("drawable/" + imageName,
null, context.getPackageName());
}
return context.getResources().getIdentifier("drawable/" + imageName,
null, context.getPackageName());
replace this by
return context.getResources().getIdentifier(imageName,
"drawable", context.getPackageName());
Hi I am new into android development and I am having trouble into this one. I wanted to display the src name of my imageView on a textView when I click a certain Image.
To understand much further here is my hardcode for the image clickListener (hope someone can also help me to improve my coding).
int ctr = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
//DECLARE CLICK LISTENERS ON OBJECTS
ImageView ImageView1 = (ImageView)findViewById(R.id.imageView1);
ImageView1.setOnClickListener(this);
ImageView ImageView2 = (ImageView)findViewById(R.id.imageView2);
ImageView2.setOnClickListener(this);
...
public void onClick(View v) {
//CHANGE TEXT VIEW TEXT BASED ON THE CTR VALUE
TextView TextView1 =(TextView)findViewById(R.id.textView1);
//I WANT TO CHANGE THIS ONE TO DISPLAY SRC INSTEAD OF THE CTR VALUE
TextView1.setText(Integer.toString(ctr));
}
There, instead of displaying the ctr value I wanted to display the src name of whatever image I click between the two images I set. I believe it is possible. Thanks.
in xml you can add tag to set any information with image
Example
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageview1"
android:background="#drawable/bg"
android:tag="bg"/>
Dynamically you can use imageView.setTag("any name") to set any data along with image and imageView.getTag() to retrieve image information
String backgroundImageName = (String) imageView.getTag();
You can use below method for get name of resource from it's id:
/**
* Determines the Name of a Resource,
* by passing the <code>R.xyz.class</code> and
* the <code>resourceID</code> of the class to it.
* #param aClass : like <code>R.drawable.class</code>
* #param resourceID : like <code>R.drawable.icon</code>
* #throws IllegalArgumentException if field is not found.
* #throws NullPointerException if <code>aClass</code>-Parameter is null.
* <code>String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);</code><br>
* Then <code>resName</code> would be '<b>icon</b>'.*/
public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
throws IllegalArgumentException{
/* Get all Fields from the class passed. */
Field[] drawableFields = aClass.getFields();
/* Loop through all Fields. */
for(Field f : drawableFields){
try {
/* All fields within the subclasses of R
* are Integers, so we need no type-check here. */
/* Compare to the resourceID we are searching. */
if (resourceID == f.getInt(null))
return f.getName(); // Return the name.
} catch (Exception e) {
e.printStackTrace();
}
}
/* Throw Exception if nothing was found*/
throw new IllegalArgumentException();
}
For Example of use :
String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);
Result will be icon
try this
String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);