How to send image to another activity in android? - android

I have a set of array which i am displaying in imageview during a specified interval of time.What i want when i click image i want to show that image in another activity.How can i do that
code:-
int[] imageArray = {R.drawable.amazon, R.drawable.app_me,
R.drawable.borabora, R.drawable.dubai};
public void showImage(){
m_oHandler = new Handler();
Runnable oRunnable = new Runnable() {
int i = 0;
#Override
public void run() {
img.setImageResource(imageArray[i]);
i++;
if (i > imageArray.length - 1) {
i = 0;
}
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Didn't know where to go
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", imageArray);
}
});
m_oHandler.postDelayed(this, 6000);
}
};
m_oHandler.postDelayed(oRunnable, 6000);
}

Do Not Pass Bitmap object.
Instead pass id of drawable resources.
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("image_res_ids", CollectonUtils.join("/", imageArray);
And in Second Activity,
getIntent().getExtra("image_res_ids");
to get image resource id array.

want when i click image i want to show that image in another activity.
Use img setTag() method for saving current drawable id and get it back on click of view using getTag()
1. set drawable id using setTag:
....
img.setImageResource(imageArray[i]);
img.setTag(imageArray[i]);
....
2. Get id from v inside onClick method:
int click_drawable_id=Integer.parseInt(v.getTag().toString());
intent.putExtra("clicked_img_draw_id", click_drawable_id);
Now instead of passing imageArray Array, just use clicked_img_draw_id to get drawable id and pass it to setImageResource to show clicked image in another Activity.

Your images are drawable resources, which are nothing more that an int. So you can definitely pass them as extras in an Intent.

You can send the array as an extra and then fetch it with getIntent().getIntArrayExtra(EXTRA_IMAGE)
For more details have a look at Intents

you save the index/resource id using setTag i.e do img.setTag(index) as follows.
And fowarding it you can remove the index/resource id from tag and pass as intent extra
#Override
public void run() {
img.setImageResource(imageArray[i]);
img.setTag(i)
i++;
//your code
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Didn't know where to go
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("IMAGE_RESOURCE", img.getTag());
}
});

just try to send position of your selected image
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
get intent
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
Check this for more http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

If you want to pass single image to next activity, than in your case, only pass image index like
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", imageArray[i]);
and on next activity get it. like
int image_id= getIntent().getIntExtra("BitmapImage",0);
this image_id is your selected image resouce id and you can set this as setImageresouce to image view.

previewView = (ImageView) findViewById(R.id.imgPostIssue);
First Activity
Intent intent = new Intent(AddNewIssue.this, PostNewIssue.class);
intent.putExtra("picture", byteArray);
finish();
startActivity(intent);
Second Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
byte[] byteArray = extras.getByteArray("picture");
if (byteArray != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imgPostIssue);
image.setImageBitmap(bmp);
}
}
Try this out, it will definitely work.. Hope it will help you.

Try using setTag(). Check this out
BitmapCache.java
public class BitmapCache {
private static SparseArray<Bitmap> _bitmapCache = new SparseArray<>();
public static void fillBitmapCache(#NonNull Resources resources) {
if (null == _bitmapCache)
_bitmapCache = new SparseArray<>();
if (_bitmapCache.indexOfKey(R.drawable.amazon) < 0)
_bitmapCache.append(R.drawable.amazon, BitmapFactory.decodeResource(resources, R.drawable.amazon));
if (_bitmapCache.indexOfKey(R.drawable.app_me) < 0)
_bitmapCache.put(R.drawable.app_me, BitmapFactory.decodeResource(resources, R.drawable.app_me));
if (_bitmapCache.indexOfKey(R.drawable.borabora) < 0)
_bitmapCache.put(R.drawable.borabora, BitmapFactory.decodeResource(resources, R.drawable.borabora));
if (_bitmapCache.indexOfKey(R.drawable.dubai) < 0)
_bitmapCache.put(R.drawable.dubai, BitmapFactory.decodeResource(resources, R.drawable.dubai));
}
public static Bitmap getAt(int position) {
return get(keyAt(position));
}
public static int keyAt(int position) {
return _bitmapCache.keyAt(position);
}
public static Bitmap get(#DrawableRes int resId) {
return _bitmapCache.get(resId);
}
public static boolean has(#DrawableRes int resId) {
return _bitmapCache.indexOfKey(resId) < 0;
}
public static void append(#DrawableRes int resId, #NonNull Resources resources) {
_bitmapCache.append(resId, BitmapFactory.decodeResource(resources, resId));
}
public static void put(#DrawableRes int resId, #NonNull Resources resources) {
_bitmapCache.put(resId, BitmapFactory.decodeResource(resources, resId));
}
public static int size() {
return _bitmapCache.size();
}
}
Activity First
private void showImage() {
BitmapCache.fillBitmapCache(getResources());
m_oHandler = new Handler();
Runnable oRunnable = new Runnable() {
int i = 0;
#Override
public void run() {
img.setImageBitmap(BitmapCache.getAt(i));
img.setTag(BitmapCache.keyAt(i));
i++;
if (i >= BitmapCache.size()) {
i = 0;
}
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null != v.getTag()) {
int resId = (int) v.getTag();
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", resId);
}
}
});
m_oHandler.postDelayed(this, 6000);
}
};
m_oHandler.postDelayed(oRunnable, 6000);
}
Second Activity
private void displayImageOnSecond() {
Bundle bundle = getIntent().getExtras();
int resId = bundle.getInt("BitmapImage", 0);
if (resId > 0) {
secondImage.setImageBitmap(BitmapCache.get(resId));
}
}
Also I recommend to use ViewPager and customize it for auto rotate rather than the one you using currently. Runnable may cause memory leaks if Activity has been destroyed

Related

Set the toolbar color, which is set in the card

I have RecyclerView + cards. In the cards I set the random color in class ViewHolder -
` int[] androidColors = view.getResources().getIntArray(R.array.androidColors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
if (frameLayout != null) {
frameLayout.setBackgroundColor(randomAndroidColor);
}`
By clicking on the card, open activity. And the color of her toolbar should be like that of a card.
Intent putExtra I can not use here, because in the intent, I already pass the tag.
public ViewHolder(View itemView) {
....
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
long poemId = (Long) v.getTag();
onPoemClickListener.onPoemClick(poemId);
}
});
}
}
public interface OnPoemClickListener {
void onPoemClick(long poemId);
}
fragment class where recycler is located
private final PoemsAdapter.OnPoemClickListener onPoemClickListener = new PoemsAdapter.OnPoemClickListener() {
#Override
public void onPoemClick(long poemId) {
Intent intent = new Intent(getActivity(), ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_POEM_ID, poemId);
startActivity(intent);
}
};
How can I get the random color that I generate in the adapter in activity? Thank!
You can put as many extras as you want:
Intent intent = new Intent(getActivity(), ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_POEM_ID, poemId);
intent.putExtra("mycolor", color);
startActivity(intent);
then get it in the activity that opens, just like you get any other extra value,

How To Set Image From Another Activity's String

I'm trying set Image from another activity's string.
My string on MainActivity
public static String imagee; // -- this is out of onCreate
capeMod.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
MainActivity.imagee = "drawable/pc";
Intent intent = new Intent(context, ModActivity.class);
startActivity(intent);
}
});
On another activity: (yeah, this is completely wrong.)
String imagee = MainActivity.imagee;
private void showImage() {
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(imagee, null, getPackageName());
Drawable imagee = getResources().getDrawable(imageResource);
image.setImageDrawable(image);
}
I can set TextView from another activity's string but couldnt set ImageView.How can i solve issue?
Instead of using a String I suggest using the id (which is an int) directly
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Button capeMod = (Button)findViewById(R.id.capemod);
capeMod.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, ModActivity.class);
intent.putExtra(ModActivity.EXTRA_DRAWABLE_ID, R.drawable.pc);
startActivity(intent);
}
}
}
}
ModActivity:
public class ModActivity extends Activity {
public static final String EXTRA_DRAWABLE_ID = "drawableId";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout2);
Bundle bundle = getIntent().getExtras();
int drawableId = bundle.getInt(EXTRA_DRAWABLE_ID);
ImageView imageView = (ImageView)findViewById(R.id.myImageView);
imageView.setImageResource(drawableId);
}
}
You can use the Intent.putExtra() to send data as well as String, and in other Activity use Intent.getExtras() to receive the data. Read more here
There is a way to do it. You need to place variable name as
imagee = "pc"; //inside drawable folder.
int resourceId = Activity.getResources().getIdentifier(imagee , "drawable", "your.package.name"); // package name can get by code also.
Drawable imagee = getResources().getDrawable(imageResource);
image.setImageDrawable(image);
Try this code that will work.
You should use something like this:
assuming that you can access to the string value doing like you did with the static string, use resources:
Resources res = getResources();
String imageName= MainActivity.imagee;
int resID = res.getIdentifier(imageName, "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
image.setImageDrawable(drawable);
EDIT: even if it works, the correct way to access a value is not through static way, is always better to go via Intents and putExtra :)

Use Image Gallery as a menu

I'm trying to use an image gallery for my application menu. The objective is that when the user click on a image it will send you to a particular activity. The problem is that I donĀ“t know how to associate each image with each activity. For example if you click on the first image it opens a game, if you click on the second one you go to the application options... How can I do it?
public class Carrusel extends Activity implements OnClickListener {
ImageView lastClicked = null;
int padding = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
LinearLayout l;
l = (LinearLayout) findViewById(R.id.carrusel);
int[] images = new int[] { R.drawable.image1, R.drawable.image2,
R.drawable.image3,R.drawable.image4,R.drawable.image5};
for (int i = 0; i <5; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(images[i]);
iv.setPadding(padding, padding, padding, padding);
iv.setOnClickListener(this);
l.addView(iv);
}
}
#Override
public void onClick(View v) {
Intent i= new Intent (this, Flip3d.class);
startActivity (i);
}
}
The last "onClick" was a test of what I was trying. Obviously in this case all the images open the same activity, that is what I want to change.
Create on OnClickListener for each of your ImageViews.
iv.setOnClickListener (new OnClickListener() {
#Override
OnClick(View v) {
// start the activity you want
}
});
You can set ids for your imageview which can be used in onClick to identify your view.
protected void onCreate(Bundle savedInstanceState) {
// ...
for (int i = 0; i <5; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(images[i]);
iv.setPadding(padding, padding, padding, padding);
iv.setOnClickListener(this);
iv.setId(mIvIds[i]);
l.addView(iv);
}
}
public void onClick(View v) {
if (v.getId() == mIvIds[0]) {
Intent i= new Intent (this, Flip3d.class);
startActivity (i);
} else if (v.getId() == mIvIds[1]) {
// other stuff
} else if (v.getId() == mIvIds[2]) {
// ...
}
}
But firstly you need to generate new ids for mIvIds[]. In API17 was View.generateViewId() added for this feature. But if only your imageviews are attached to that onclick listener i think there should be no problem if you use some random integers (e.g. 1,2,3,... )

How to pass values statically from one page to other in android?

In my projects I kept different images for categories. When I click on each category image I am passing its category id to other page statically and setting the drawable image of that category in the new page.
My Code:
Firstpage.java
public static String categoryid;
category1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="0";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
category2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="1";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
category3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="2";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
Nextpage.java
public static String catid = Firstpage.categoryid;
ImageView categorytype=(ImageView)findViewById(R.id.imageView1);
if(catid=="0")
{
categorytype.setBackgroundResource(R.drawable.image1);
}
else if(catid=="1")
{
categorytype.setBackgroundResource(R.drawable.image2);
}
else if(catid=="2")
{
categorytype.setBackgroundResource(R.drawable.image3);
}
First time when I am clicking on the category image it is passing the category id to the next page and that particular image is setting in the nextpage. After that I clicked the back button(android back button) and went to Firstpage.java and again clicked on other image. But this time also the same image stored. The category id didnt changed. The category id is not refreshing...How to refresh the category id? Any suggestion will be thankful.....
You are comparing two strings by == operator, instead you should compare by equals method, try following:
if(catid.equals("0"))
{
categorytype.setBackgroundResource(R.drawable.image1);
}
else if(catid.equals("1"))
{
categorytype.setBackgroundResource(R.drawable.image2);
}
else if(catid.equals(2"))
{
categorytype.setBackgroundResource(R.drawable.image3);
}
Don't use static variables for this. Pass the category ID to your Nextpage activity through the intent. In Firstpage.java:
public static final String CATEGORY_KEY = "category";
category1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(this,Nextpage.class);
myIntent.putExtra(CATEGORY_KEY, "0");
startActivityForResult(myIntent, 0);
}
});
Then retrieve it in the onCreate(Bundle) method of Nextpage.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String categoryid = intent.getStringExtra(Firstpage.CATEGORY_KEY);
. . .
}
The reason your method doesn't work is that Nextpage.catid is initialized when the class is loaded, but the assignment statement is not executed again unless the class is unloaded and needs to be reloaded.
I think problem is here you are comparing String values using this if(catid=="0") Which you should be compare it using if(catid.trim().equals("0"))
Update this change in your code and check it.

Android - set random button image clickable

I'm having touble in making my random generated image (which is read as a button) become clickable which leads to each different activity for each different image. So the random images work perfect actually, the only problem it's not clickable Here's my code
final Button imgView = (Button)findViewById(R.id.top1);
Random rand = new Random();
int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
On my layout, I specified the id top1 as a button.
So the above code will look up to my drawable images, which have the names 'img1.jpg', 'img2.jpg', 'img3.jpg' , and 'img4.jpg'.
So what I wanna make is something like, when 'img1.jpg' is generated, it becomes clickable and leads to for example: Activity1.java , for 'img2.jpg' the intent goes to 'Activity2.java', etc.
Thanks much in advance. I'm open for any kind of solution :)
UPDATED:
Here's the full code of my Main class:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_x);
final Button imgView = (Button)findViewById(R.id.top1);
Random rand = new Random();
imgView.setOnClickListener(new ActivitySwitch(1,this));
imgView.setOnClickListener(new ActivitySwitch(2,this));
imgView.setOnClickListener(new ActivitySwitch(3,this));
imgView.setOnClickListener(new ActivitySwitch(4,this));
int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
}
and here the ActivitySwitch class:
public class ActivitySwitch implements OnClickListener{
int imageNo;
Context context;
public ActivitySwitch(int imageNo,Context context) {
super();
this.context=context;
this.imageNo = imageNo;
}
#Override
public void onClick(View v) {
Intent it=new Intent();
if(imageNo==1)
{
it.setClass(context, ProjektAID.class);
}
else if (imageNo==2)
{
it.setClass(context, ProjektADH.class);
}
else if (imageNo==3)
{
it.setClass(context, ProjektBOS.class);
}
else if (imageNo==4)
{
it.setClass(context, ProjektBROT.class);
}
}
}
There is a way: create a new class
class ActivitySwitch implements OnClickListener{
int imageNo;
Activity context
public ActivitySwitch(int imageNo,Context context) {
super();
this.context=(Activity)context;
this.imageNo = imageNo;
}
#Override
public void onClick(View v) {
Intent it=new Intent();
if(imageNo==1){
it.setClass(context, Activity1.class);
}
else{
.......
}
startActivityForResult(it,any_integer_value);
}
}
And then in the Activity set:
imgView.setOnClickListener(new ActivitySwitcher(randInt,this);
if this is a button, simple implement the function of onclicklistener of button and call the respective activity from text it gets... ping me if u didn't understand..

Categories

Resources