I tried to convert a TextView into Bitmap and I tried to pass it to another Activity on a single Button click.
My code is:
Activity sending the Bitmap :
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view0 = txt1.getRootView(); //txt1 is a TextView
view0.setDrawingCacheEnabled(true);
view0.buildDrawingCache();
Bitmap bmp0 = Bitmap.createBitmap(view0.getDrawingCache());
Intent in = new Intent(Meme_make.this,S_meme.class);
in.putExtra("bm0" , bmp0);
startActivity(in);
}
Activity Receiving the Bitmap :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sm);
main1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = getIntent().getParcelableExtra("bm0");
main1.setImageBitmap(bitmap);
}
But when I click on the button , the activity is automatically closing and taking me back to the Main launcher Activity.
How to do this without any issues ?
You have
in.putExtra("bm0" , bmp0);
and
Bitmap bitmap = getIntent().getParcelableExtra("bmp0");
In particular bm0 vs bmp0, you may just be missing a p.
But in general I would also cast your retrieved bitmap to a bitmap
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("bmp0");
Try this code snippet.
Send bitmap
Intent intent = new Intent(this, ActivityName.class);
intent.putExtra("Bitmap", bitmap);
Retrieve Bitmap
Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("Bitmap");
Related
I saw a video of how to put an camera access on android studio but I didn´t have much space in my current activity so I made an clicklistener on the image and when the user clicks on image, it goes to another activity where the image appears bigger, but when I click on the image it goes to the other acivity but doesn´t show the image. I saw other questions but or they ask about how to pass an image that is on drawable resources or the answer doesn´t fit me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fazer_perguntas);
photoCamera = (RelativeLayout) findViewById(R.id.photoCamera);
photo = (ImageView) findViewById(R.id.photo);
photo2 = (ImageView) findViewById(R.id.photo2);
photo3 = (ImageView) findViewById(R.id.photo3);
photoCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera,0);
}
});
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent p = new Intent(getApplicationContext(),BigPhoto.class);
p.putExtra("idphoto",photo.getId());
p.putExtra("idphoto2",photo2.getId());
p.putExtra("idphoto3",photo3.getId());
startActivity(p);
}
});
{
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
photo.setImageBitmap(bitmap);
Intent selfiSrc = new Intent(this, BigPhoto.class);
selfiSrc.putExtra("data",bitmap);
startActivity(selfiSrc);
}
}
}
This is part of my firstactivity java class. photo2 and photo3 are other imageviews that I will do the same thing for photo.
package com.example.ritalopes.help;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class BigPhoto extends AppCompatActivity {
ImageView photo, photo2, photo3, bigphoto;
private Uri path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big_photo);
photo = (ImageView) findViewById(R.id.photo);
photo2 = (ImageView) findViewById(R.id.photo2);
photo3 = (ImageView) findViewById(R.id.photo3);
bigphoto = (ImageView) findViewById(R.id.bigphoto);
Intent pho = getIntent();
int image1 = pho.getIntExtra("idphoto",0);
int image2 = pho.getIntExtra("idphoto2",0);
int image3 = pho.getIntExtra("idphoto3",0);
Intent take = getIntent();
Bitmap bitmap = (Bitmap) take.getParcelableExtra("data");
bigphoto.setImageBitmap(bitmap);
}}
This is my secondactivity javaclass where I want to pass the image(s) to. I tried that code envolving a Uri but didn´t worked out.
Thanks in advance for your responses.
Your code is not very clear...
First, you have to add an OnClickListener for all images, not only for photo
Then, you have to pass extra data to Intent, like you've done. But, don't pass the ID of the imageView but the drawable ID you want to show in fullscreen.
Do you want to display images for drawable ? Or Bitmaps ? If bitmap, I think intent.putExtra("data", bitmap) will work as Bitmap is Parcelable.
if your purpose is sending an image in order to preview image in the another activity. I recommend you try to use this library. just add it in your gradle file in your project.
https://github.com/chathuralakmal/AndroidImagePopup
this preview image library have using Picasso Library to you can show your preview is so easy. and it also contain close button for you.
it help me , i hope this would save you time.
I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
My app is supposed to first displays an image, when a button is pressed, another image is displayed in the image view. However, when the screen is rotated, the imageView displays the old image. How should I go about fixing this? (bits is the Bitmap loaded into imageView on create)
My code is below:
RGBToBitmap(rgb.getWindow(), bits); //this loads a new image into bits
imageView.setImageBitmap(bits);
I suppose you are setting the first image of your ImageView in the onCreate or onStart method of your Activity.
Upon rotating the screen, the onCreate and onStart methods get called again, and therefore your ImageView displays the first image again.
In order to save your Activity state, have a look at this:
http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
This could be a possible solution:
Bitmap image = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = (Bitmap) getLastNonConfigurationInstance();
if(bitmap == null){
image = downloadImage();
}
setImage(bitmap);
}
#Override
public Object onRetainNonConfigurationInstance() {
return bitmap;
}
You can also read into this method when ur using fragments:
Fragment.setRetainInstance
Or the Bundle: (onSaveInstanceState(Bundle) is also available in your Activity)
//Save it onSaveInstanceState:
#Override
public void onSaveInstanceState(Bundle toSave) {
super.onSaveInstanceState(toSave);
toSave.putParcelable("bitmap", bitmap);
}
//nd get it back onCreate:
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
if (savedState != null) bitmap = savedState.getParcelable("bitmap");
}
I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
I am developing playing video application and taking screenshot of running video and display a screenshot in next activity, i am playing video and taking screenshot and i am not able to display screenshot in next activity please check my code and give me changes.
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.ImageView01);
// image.setBackgroundDrawable(bitmapDrawable);
String bitmap = image.toString();
System.out.println("Image getting++++++ : " + bitmap);
Intent intent = new Intent(VideoDemo.this, ScreenshotView.class);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
public class ScreenshotView extends Activity
{ private String filename;
private ImageButton back;
private ImageView screenshot;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.screenshot);
screenshot =(ImageView)findViewById(R.id.screen);
back = (ImageButton)findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
System.gc();
Intent i = getIntent();
Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");
screenshot.setImageBitmap(bitmap);
}
}
Here your "bitmap" object is a string.
And you are passing a string object to your next activity.
That is why, you are not able to set image in you ImageView screenshot.
Can you try the below code and lemme know whether you fixed it.
Sending Object
Here is the code to send the Object from one to other class. One Important thing to send the Object is the class should implement the Serializable class.
The below Red Colored text should be same.
//MainActivity.java
Intent i = new Intent(MainActivity.this,startActivity.class);
ObjectClassName object = new ObjectClassName();
i.putExtra("THIS", Object);
Receiving Object
// startActivity.java
Intent i = getIntent();
ObjectClassName obj = (ObjectClassName) getIntent().getSerializableExtra("THIS");//
TypeCasting needed