How to send image from one activity to another in android?

ⅰ亾dé卋堺 提交于 2019-12-17 19:53:41

问题


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


回答1:


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.




回答2:


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?




回答3:


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();



回答4:


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);



回答5:


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 ;




回答6:


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);



回答7:


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




回答8:


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).



来源:https://stackoverflow.com/questions/11598838/how-to-send-image-from-one-activity-to-another-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!