Image passing from one activity to another using extras - android

Would anyone tell me how to pass images from one activity to another activity using extras (the idea is). I am displaying a set of images in activity1 as a horizontal scroll view. When I click on an image it should be displayed in another activity (activity2) with totally different layout.
I'll be satisfied if the explanation is done with json as well I have tried this (http://www.vogella.com/tutorials/AndroidIntent/article.html) tutorial but can't really find the answer.
MAIN ACTIVITY:
package com.example.user.horizontal;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
ImageView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView im1 = (ImageView) findViewById(R.id.image1);
ImageView im2 = (ImageView) findViewById(R.id.image2);
ImageView im3 = (ImageView) findViewById(R.id.image3);
ImageView im4 = (ImageView) findViewById(R.id.image4);
ImageView im5 = (ImageView) findViewById(R.id.image5);
ImageView im6 = (ImageView) findViewById(R.id.image6);
ImageView im7 = (ImageView) findViewById(R.id.image7);
im1.setOnClickListener(this);
im2.setOnClickListener(this);
im3.setOnClickListener(this);
im4.setOnClickListener(this);
im5.setOnClickListener(this);
im6.setOnClickListener(this);
im7.setOnClickListener(this);
}
public void onClick(View v) {
Intent clickimage = new Intent(this, OnClick.class);
switch (v.getId()) {
case R.id.image1:
Display.setImageResource(R.drawable.images1);
clickimage.putExtra("display1", "images1");
startActivity(clickimage);
break;
case R.id.image2:
Display.setImageResource(R.drawable.images2);
clickimage.putExtra("display2", "pic2filename");
startActivity(clickimage);
break;
case R.id.image3:
Display.setImageResource(R.drawable.images8);
clickimage.putExtra("display3", "pic3filename");
startActivity(clickimage);
break;
case R.id.image4:
Display.setImageResource(R.drawable.images4);
clickimage.putExtra("display4", "pic4filename");
startActivity(clickimage);
break;
case R.id.image5:
Display.setImageResource(R.drawable.images5);
clickimage.putExtra("display5", "pic5filename");
startActivity(clickimage);
break;
case R.id.image6:
Display.setImageResource(R.drawable.images6);
clickimage.putExtra("display6", "pic6filename");
startActivity(clickimage);
break;
case R.id.image7:
Display.setImageResource(R.drawable.images8);
clickimage.putExtra("display7", "pic7filename");
startActivity(clickimage);
break;
}
}
SECOND ACTIVITY :
package com.example.user.horizontal;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class OnClick extends Activity{
private Bitmap mImage1;
private Bitmap mImage2;
private Bitmap mImage3;
private Bitmap mImage4;
private Bitmap mImage5;
private Bitmap mImage6;
private Bitmap mImage7;
private Bitmap mImage8;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.onclick);
Bundle bundle = getIntent().getExtras();
String imageName = bundle.getString("display");
// click listeners
ImageView imageView = (ImageView)findViewById(R.id.image1);
mImage1 = BitmapFactory.decodeResource(getResources(), R.drawable.images1);
mImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.images2);
mImage3 = BitmapFactory.decodeResource(getResources(), R.drawable.images4);
mImage4 = BitmapFactory.decodeResource(getResources(), R.drawable.images5);
mImage5 = BitmapFactory.decodeResource(getResources(), R.drawable.images6);
mImage6 = BitmapFactory.decodeResource(getResources(), R.drawable.images7);
mImage7 = BitmapFactory.decodeResource(getResources(), R.drawable.images8);
mImage8 = BitmapFactory.decodeResource(getResources(), R.drawable.images);
if(imageName.matches("images1")) {
String b = bundle.getString("b");
imageView.setImageBitmap(mImage1);
}
else if (imageName.matches("pic2filename")) {
imageView.setImageBitmap(mImage2);
}
else if (imageName.matches("pic3filename")) {
imageView.setImageBitmap(mImage3);
}
else if (imageName.matches("pic4filename")) {
imageView.setImageBitmap(mImage4);
}
else if (imageName.matches("pic2filename")) {
imageView.setImageBitmap(mImage5);
}
else if (imageName.matches("pic5filename")) {
imageView.setImageBitmap(mImage6);
}
else if (imageName.matches("pic6filename")) {
imageView.setImageBitmap(mImage7);
}
else if (imageName.matches("pic7filename")) {
imageView.setImageBitmap(mImage8);
}
}
}
I am new to Java and Android.

Try passing the image file path or image url to next activity using extras in android

Pass Bitmap using intent :
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Don't pass images between activities.
You should pass the path to image in Intent and show it in your new Activity. This is a correct way to do it.

Related

send image as a bit map (By Intent or Sharedpreference)

I want to pass image in one activity to another activity.I tried many time.but my app gonna crash.plz edit my code with necessary changes.
Sender activity:
camera=(ImageButton)findViewById(R.id.camera);
gallery=(ImageButton)findViewById(R.id.gallery);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(i, 50);
}
});
gallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 40);
}
});
}
Receiver activity:
package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_set_);
ImageView IV = (ImageView) findViewById(R.id.simpleImageView);
}
}
in onActivityResult
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.d("===uploadPhoto","camera : "+photo);
*//* Passing BITMAP to the Second Activity *//*
Intent intentCamera = new Intent(this, SecondAvtivity.class);
IntentCamera.putExtra("BitmapImage", photo);
startActivity(intentCamera);
in seconActivity
Bitmap fromCamera = getIntent().getParcelableExtra("BitmapImage");
imageView.setImageBitmap(fromCamera)
Try to Use This Code
Frist Classs
ImageView img=findViewById(R.id.imgId);
BitmapDrawable bmd= (BitmapDrawable) img.getDrawable();
Bitmap bt= bmd.getBitmap();
final byte[] imgarry = bt.getNinePatchChunk();
Intent intent = new Intent (FirstClass.this,SecondClass.class);
Intent.putExtra(“imgs”,imgarry);
startActivity(intent);
Second Class
Intent in = getIntent();
byte[] ary= in.getByteArrayExtra(“imgs”);
Bitmap bm= BitmapFactory.decodeByteArray(ary,0,ary.length);
imageView.setImageBitmap(bm);

How to send image one activity to another activity?

I am new in android.i want to pass image in one activity to another activity.I tried many time.but my app gonna crash.plz edit my code with necessary changes.
Sender activity:
camera=(ImageButton)findViewById(R.id.camera);
gallery=(ImageButton)findViewById(R.id.gallery);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(i, 50);
}
});
gallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 40);
}
});
}
Receiver activity:
package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_set_);
ImageView IV = (ImageView) findViewById(R.id.simpleImageView);
}
}
Try to override onActivityResult(). For further reading - https://developer.android.com/training/basics/intents/result
Try this code For sending the image
ImageView imageview = (ImageView) findViewById(R.id.Tab);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
final byte[] arr = bitmap.getNinePatchChunk();
imageView.setOnClickListener(View v)
{
Intent intent = new Intent (MainActivity.this,bbb.class);
Intent.putExtra(“image”,arr);
startActivity(intent);
}
For getting the image
Intent I = getIntent();
byte[] arr1 = i.getByteArrayExtra(“image”);
Bitmap map = BitmapFactory.decodeByteArray(arr,0,arr.length);
imageView.setImageBitmap(map);

Get ImageView without knowing about it

I have custom Dialog that show 4 ImageViews (All Photos, Favorites, Taged, Albums).
The layout is something like:
<LinearLayout>
<ImageView>...1</ImageView>
<ImageView>...2</ImageView>
<ImageView>...3</ImageView>
<ImageView>...4</ImageView>
</LinearLayout>
The constructor contains this code:
iv = new ImageView[3];
iv[0] = (ImageView) findViewById(R.id.btnPhotoAll);
iv[1] = (ImageView) findViewById(R.id.btnPhotoFavorites);
iv[2] = (ImageView) findViewById(R.id.btnPhotoTags);
iv[3] = (ImageView) findViewById(R.id.btnPhotoAlbums);
for (ImageView image : iv){
image.setOnClickListener(new DialogPhotoOnItemClickListener(context, image));
}
The listener is:
package com.kappa.cloudgallery.listeners;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.kappa.cloudgallery.R;
import com.kappa.cloudgallery.widgets.DialogPhoto;
public class DialogPhotoOnItemClickListener implements View.OnClickListener
{
Context context;
ImageView iv;
public DialogPhotoOnItemClickListener(Context context, ImageView imageView){
this.context = context;
this.iv = imageView;
}
#Override
public void onClick(View view)
{
switch (iv.getId())
{
case R.id.btnPhotoAll:
Toast.makeText(context, "All", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoFavorites:
Toast.makeText(context, "Favorites", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoTags:
Toast.makeText(context, "Tags", Toast.LENGTH_LONG).show();
break;
case R.id.btnPhotoAlbums:
Toast.makeText(context, "Albums", Toast.LENGTH_LONG).show();
break;
}
}
}
The problem is that i pass the imageview in constructor. There is any efficient way to get which image i clicked?
Dialog: enter image description here
Problem is here:
#Override
public void onClick(View view){
switch (iv.getId()){
}
}
You are using iv.getId() instead of this use view.getId(). And you don't need to pass Imageview in constructor.

Set image as Background Onclick Android

I am working on a project in which click on an image from gridview opens that image in an ImageView , i got this code from this website http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ , so i modified the FullImageActivity.java and added a button to set the image as background, but everytime i click on the button, it just force closes my app, P.S i have added the permission in the manifest also
here is my code
package com.example.androidhive;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
private WallpaperManager imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
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]);
}
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
if(bitmap!=null)
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
I think you may also be trying to use it like this:
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
mImageView = (ImageView) findViewById(R.id.full_image_view);
mImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageAdapter.mThumbIds[position]));
}
public void setaswall(View view) {
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
final Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
if (bitmap != null) {
wm.setBitmap(bitmap);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
And make sure you've included the set wallpaper permission in your AndroidManifest.
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Here you declare a field with the name "imageView", but of type WallpaperManager:
private WallpaperManager imageView;
then later you declare some local imageView:
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
last not least you are trying to get a Drawable from the field imageView that you have never initialized:
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
This will throw an exception which you later catch and print the stacktrace
==> Solution: initialize the field imageView properly and don't create local var that hides the field.
It is simple I think you may forgot add the Permission in your Mainfest file you
just add this in your Mainfest file
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

Android: Orientation changes erase modifications made to my ImageView

I have app in which I have ImageView. I open new activity, where I paint something by finger and this bitmap return to my ImageView. everything is ok but when I change orientation now, my activity with ImageView is repaint or restart and imageview is empty. I try everything, I try SaveState and restore state, try configurationChange, and other, but nothing is working...
package jilova.Android.TextFolder;
import java.io.ByteArrayOutputStream;
import jilova.Android.Enums;
import jilova.Android.R;
import jilova.Android.DatabaseFolder.LocalDB;
import jilova.Android.DatabaseFolder.RequestRow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;
public class Text extends Activity{
private static EditText t1;
private static EditText t2;
private static EditText t3;
private static EditText t4;
private static ImageView iv1;
private static Context c;
private static Activity ac;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Object o = this.getLastNonConfigurationInstance();
if(o!=null)
{
Enums.sign=(Bitmap)o;
}
c=this.getApplicationContext();
ac=this;
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
iv.setDrawingCacheEnabled(true);
t1 = (EditText)this.findViewById(R.id.TEXTNote);
t2 = (EditText)this.findViewById(R.id.TextET2);
t3 = (EditText)this.findViewById(R.id.TextET3);
t4 = (EditText)this.findViewById(R.id.TextET4);
//iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false).copy(Config.ARGB_8888, true));
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
}
}
public static RequestRow getData()
{
RequestRow row = new RequestRow();
row.SetREQUESTNOTE(t1.getText().toString());
row.SetREQUESTTYPEID(Integer.parseInt(t2.getTag().toString()));
row.SetTYPTECHUDRZBYID(Integer.parseInt(t3.getTag().toString()));
row.SetOBJECTID(Integer.parseInt(t4.getTag().toString()));
return row;
}
public static void setData(String REQUESTTYPEID ,String TYPTECHUDRZBYID,String OBJECTID,String REQUESTNOTE,String REQUESTID)
{
t1.setText(REQUESTNOTE);
t2.setTag(REQUESTTYPEID);
t2.setText(LocalDB.dbGetRequestTypeByID(c, Integer.parseInt(REQUESTTYPEID)));
t3.setTag(TYPTECHUDRZBYID);
t3.setText(LocalDB.dbGetTypTechUdrzbyByID(c, Integer.parseInt(TYPTECHUDRZBYID)));
t4.setTag(OBJECTID);
t4.setText(LocalDB.dbGetObjectByID(c, Integer.parseInt(OBJECTID)));
Bitmap b = LocalDB.dbGetDocumentByID(c, Integer.parseInt(REQUESTID));
if(b!=null)
{
iv1.setImageBitmap(b);
}
}
public static Bitmap getSign()
{
iv1.buildDrawingCache();
Bitmap ret = iv1.getDrawingCache();
if(Enums.EmptySignHash)
{
return null;
}
else
{
return ret;
}
}
public void Sign(View button)
{
Intent s = new Intent(Text.this,Sign.class);
startActivityForResult(s,Enums.SIGNREQUESTID);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode!=Activity.RESULT_OK)
{
return;
}
Enums.IDLocal=-1;
if(requestCode==Enums.GetData)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
EditText et = (EditText)this.findViewById(extras.getInt("ViewID"));
et.setText(extras.getString("Value"));
et.setTag(extras.get("ID"));
}
}
else if(requestCode==Enums.SIGNREQUESTID)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
try
{
byte[] b = extras.getByteArray("Bitmap");
Enums.sign = Bitmap.createBitmap(BitmapFactory.decodeByteArray(b, 0, b.length), 0, 0, ac.getWindowManager().getDefaultDisplay().getWidth(), 200);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false));
}
catch(Exception e)
{
int a=0;
}
}
Enums.EmptySignHash=false;
}
}
public static void DeleteAll()
{
EditText t = (EditText)ac.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void Delete(View button)
{
EditText t = (EditText)this.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void GetData(View button)
{
if(button.getId()==R.id.TextIB3)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select typtechudrzbyid,typtechudrzbydesc from typtechudrzby");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET3);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB2)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select Requesttypeid,requestname from requesttype");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET2);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB4)
{
Intent ChooseAction = new Intent(Text.this,TreeData.class);
Enums.TreeData = LocalDB.dbLocalSelectTree(this.getApplicationContext(),"Select objectid,objectname,objectref from Objects");
if(Enums.TreeData==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET4);
startActivityForResult(ChooseAction,Enums.GetData);
}
else
{
//throw new Exception();
}
}
}
When a configuration change such as a screen rotation occurs by default your Activity is destroyed and then recreated (onDestroy of the current activity is called, and then the onCreate of a new version of your activity is called).
You can either:
Stop Android recreating your activity when a configuration change occurs. To do this add android:configChanges="keyboardHidden|orientation" to the activity tag in your manifest. This is not recommend since if you want a different layout etc for different configurations you will have to handle changing the layout yourself.
Override onRetainNonConfigurationInstance and return your bitmap from that. In onCreate check if the last non-configuration instance is not null, in which case cast it to a bitmap and then set the image.
For the latter case, use something like the following:
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Check if our activity was just destroyed and re-created
final Object retainedFromConfigChange = getLastNonConfigurationInstance();
if (retainedFromConfigChange != null) {
// Activity has just been recreated, get the image we were working on
// before the configuration change
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
iv.setImageBitmap((Bitmap) retainedFromConfigChange);
}
...
}
#Override
public Object getLastNonConfigurationInstance() {
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
// We have to return a plain old Bitmap and not a drawable of any sorts
// or we will get memory leaks so we need to extract the bitmap from the drawable
return ((BitmapDrawable) iv.getDrawable()).getBitmap();
}
I think you forgot to override the onRetainNonConfigurationInstance() method where you return your bitmap so it will be passed to the new activity. In the new activity you can retrieve the bitmap as you already do by calling getLastNonConfigurationInstance().
When the configuration is changed, the whole view is re-created. So, we need to retain the imageView resource. The best way to do this is to handle the orientation change by creating a Retained fragment.
You can find the perfect documentation on Android developer's page.
P.S.: Adding android:configChanges="keyboardHidden|orientation" to the manifest is highly not recommended.

Categories

Resources