i am having 20 imageview and i am having 20 button for them when i click any 1 button it gives me option to select image from gallery or camera when i select any option for example galley it will take me to the gallery and let me select image from their and let me display those images on my imageview for respective button
now the problem is sometimes when i do the whole above process my activity is getting restart actuomatically and all the image which were first selected get vanished from their imageview
For Refernce my code is as follow:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_upload);
// **************Code to get Road worthy number and VIN number value in
// Shared Preference starts here************************
SharedPreferences myPrefs1 = this.getSharedPreferences("myPrefs",
MODE_WORLD_READABLE);
roadworthynumber = myPrefs1.getString(MY_ROADWORTHY,
"Road Worthy Number");
vinnumber = myPrefs1.getString(MY_VIN, "VIN Number");
// **************Code to get Road worthy number and VIN number value in
// Shared Preference ends here************************
// **************Code to create Directory AUSRWC starts
// here************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(Environment.getExternalStorageDirectory()
+ File.separator + "AUSRWC" + File.separator);
cacheDir.mkdirs();
}
// **************Code to Create Directory AUSRWC ends
// here************************
// *****************Assigning Button variable their Id declare in XML
// file starts here*****************
new_select1 = (Button) findViewById(R.id.new_select1);
new_select2 = (Button) findViewById(R.id.new_select2);
new_select3 = (Button) findViewById(R.id.new_select3);
new_select4 = (Button) findViewById(R.id.new_select4);
new_select5 = (Button) findViewById(R.id.new_select5);
new_select6 = (Button) findViewById(R.id.new_select6);
new_select7 = (Button) findViewById(R.id.new_select7);
new_select8 = (Button) findViewById(R.id.new_select8);
new_select9 = (Button) findViewById(R.id.new_select9);
new_select10 = (Button) findViewById(R.id.new_select10);
new_select11 = (Button) findViewById(R.id.new_select11);
new_select12 = (Button) findViewById(R.id.new_select12);
new_select13 = (Button) findViewById(R.id.new_select13);
new_select14 = (Button) findViewById(R.id.new_select14);
new_select15 = (Button) findViewById(R.id.new_select15);
new_select16 = (Button) findViewById(R.id.new_select16);
new_select17 = (Button) findViewById(R.id.new_select17);
new_select18 = (Button) findViewById(R.id.new_select18);
new_select19 = (Button) findViewById(R.id.new_select19);
new_select20 = (Button) findViewById(R.id.new_select20);
// *****************Assigning Button variable their Id declare in XML
// file ends here*****************
// *****************Assigning Image variable their Id declare in XML
// file starts here*****************
new_selectimage1 = (ImageView) findViewById(R.id.new_selectImage1);
new_selectimage2 = (ImageView) findViewById(R.id.new_selectImage2);
new_selectimage3 = (ImageView) findViewById(R.id.new_selectImage3);
new_selectimage4 = (ImageView) findViewById(R.id.new_selectImage4);
new_selectimage5 = (ImageView) findViewById(R.id.new_selectImage5);
new_selectimage6 = (ImageView) findViewById(R.id.new_selectImage6);
new_selectimage7 = (ImageView) findViewById(R.id.new_selectImage7);
new_selectimage8 = (ImageView) findViewById(R.id.new_selectImage8);
new_selectimage9 = (ImageView) findViewById(R.id.new_selectImage9);
new_selectimage10 = (ImageView) findViewById(R.id.new_selectImage10);
new_selectimage11 = (ImageView) findViewById(R.id.new_selectImage11);
new_selectimage12 = (ImageView) findViewById(R.id.new_selectImage12);
new_selectimage13 = (ImageView) findViewById(R.id.new_selectImage13);
new_selectimage14 = (ImageView) findViewById(R.id.new_selectImage14);
new_selectimage15 = (ImageView) findViewById(R.id.new_selectImage15);
new_selectimage16 = (ImageView) findViewById(R.id.new_selectImage16);
new_selectimage17 = (ImageView) findViewById(R.id.new_selectImage17);
new_selectimage18 = (ImageView) findViewById(R.id.new_selectImage18);
new_selectimage19 = (ImageView) findViewById(R.id.new_selectImage19);
new_selectimage20 = (ImageView) findViewById(R.id.new_selectImage20);
// ****Assigning Image variable their Id declare in XML file ends
// here*****************
// **************Creating Dialog to give option to user to new_select
// image from gallery or from camera starts here****************
final String[] items = new String[] { "From Camera", "From Gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
if (android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment
.getExternalStorageDirectory(),
"/AUSRWC/picture" + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
Toast.makeText(getBaseContext(), "Click Image",
Toast.LENGTH_SHORT).show();
intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(getBaseContext(),
"Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
dialog.cancel();
} else {
Intent intent = new Intent();
Toast.makeText(getBaseContext(), "Select Image",
Toast.LENGTH_SHORT).show();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}
}
});
dialog = builder.create();
// **************Creating Dialog to give option to user to new_select
// image from gallery or from camera ends here****************
final Animation animAlpha = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha); // Animation Code for displaying Button
// Clicked.
// ********************Image 1 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 1;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 1 button code ends
// here*******************************
// ********************Image 2 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 2;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 2 button code ends
// here*******************************
// ********************Image 3 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 3;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 3 button code ends
// here*******************************
// ********************Image 4 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 4;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 4 button code ends
// here*******************************
// ********************Image 5 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 5;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 5 button code ends
// here*******************************
// ********************Image 6 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 6;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 6 button code ends
// here*******************************
// ********************Image 7 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 7;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 7 button code ends
// here*******************************
// ********************Image 8 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 8;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 8 button code ends
// here*******************************
// ********************Image 9 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 9;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 9 button code ends
// here*******************************
// ********************Image 10 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select10.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 10;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 10 button code ends
// here*******************************
// ********************Image 11 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 11;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 11 button code ends
// here*******************************
// ********************Image 12 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select12.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 12;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 12 button code ends
// here*******************************
// ********************Image 13 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select13.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 13;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 13 button code ends
// here*******************************
// ********************Image 14 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select14.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 14;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 14 button code ends
// here*******************************
// ********************Image 15 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select15.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 15;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 15 button code ends
// here*******************************
// ********************Image 16 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select16.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 16;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 16 button code ends
// here*******************************
// ********************Image 17 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select17.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 17;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 17 button code ends
// here*******************************
// ********************Image 18 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select18.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 18;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 18 button code ends
// here*******************************
// ********************Image 19 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select19.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 19;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 19 button code ends
// here*******************************
// ********************Image 20 button code starts
// here*******************************
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
new_select20.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
buttonpressed = 20;
dialog.show();
}
});
} else {
Toast.makeText(getBaseContext(), "Please insert SdCard First",
Toast.LENGTH_SHORT).show();
}
// ********************Image 20 button code ends
// here*******************************
}
// *************************When Back Button is Pressed code begins
// here*************************************
@Override
public void onBackPressed() {
Toast.makeText(new_upload.this, "Sorry You are not allowed to go back",
Toast.LENGTH_SHORT).show();
return;
}
// *************************When Back Button is Pressed code ends
// here*************************************
// ***********************To get Path of new_selected Image code starts
// here************************************
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// ***********************To get Path of new_selected Image code ends
// here************************************
// **********************Picture obtained from the camera or from gallery
// code starts here**************
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//path = "";
Log.e("","requestCode="+requestCode);
switch (requestCode){
case PICK_FROM_FILE:
if (resultCode == Activity.RESULT_OK) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); // from Gallery
Log.e("", "Imagepath from gallery=" + path);
if (path == null)
path = mImageCaptureUri.getPath(); // from File Manager
if (path != null) {
dialog1 = ProgressDialog.show(new_upload.this, "",
"Processing Please wait...", true);
new ImageDisplayTask().execute();
}
}
break;
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
try {
path = mImageCaptureUri.getPath();
Log.e("", "Imagepath from Camera =" + path);
// bitmap = BitmapFactory.decodeFile(path);
} catch (Exception e) {
e.printStackTrace();
}
if (path != null) {
dialog1 = ProgressDialog.show(new_upload.this, "",
"Processing Please wait...", true);
//new ImageDisplayTask1().execute();
new ImageDisplayTask().execute();
}
}
break;
default:
}
}
// ********************Picture obtained from the camera or from gallery code
// ends here*********************************************
// ******************Image Display on Button when new_selected from gallery
// Ashynch Code starts here********************************
class ImageDisplayTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... unsued) {
Bitmap src = BitmapFactory.decodeFile(path);
Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);
//Bitmap dest = Bitmap.createScaledBitmap(src, src.getWidth(),src.getHeight(), true);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local ``
String timestamp = dateTime + " " + roadworthynumber;
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String dateTime1 = sdf1.format(Calendar.getInstance().getTime());
Imagename = dateTime1.toString().trim().replaceAll(":", "")
.replaceAll("-", "").replaceAll(" ", "")
+ roadworthynumber + ".jpg";
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(100);
tPaint.setTypeface(Typeface.SERIF);
tPaint.setColor(Color.RED);
tPaint.setStyle(Style.FILL);
cs.drawBitmap(src, 0f, 0f, null);
float height = tPaint.measureText("yY");
cs.drawText(timestamp, 5f, src.getHeight() - height + 5f, tPaint);
try {
dest.compress(Bitmap.CompressFormat.JPEG, 70,
new FileOutputStream(new File(cacheDir, Imagename)));
dest.recycle();
src.recycle();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String serverresponse) {
String error = "noerror";
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight() - 100;
Log.e("", "width= " + dw + " Height= " + dh);
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() + "/AUSRWC/"
+ Imagename, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) dh);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) dw);
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() + "/AUSRWC/"
+ Imagename, bmpFactoryOptions);
if (buttonpressed == 1) {
new_selectimage1.setImageBitmap(bmp); //Image set on ImageView
} else if (buttonpressed == 2) {
new_selectimage2.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 3) {
new_selectimage3.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 4) {
new_selectimage4.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 5) {
new_selectimage5.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 6) {
new_selectimage6.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 7) {
new_selectimage7.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 8) {
new_selectimage8.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 9) {
new_selectimage9.setImageBitmap(bmp);//Image set on ImageView
} else if (buttonpressed == 10) {
new_selectimage10.setImageBitmap(bmp);
} else if (buttonpressed == 11) {
new_selectimage11.setImageBitmap(bmp);
} else if (buttonpressed == 12) {
new_selectimage12.setImageBitmap(bmp);
} else if (buttonpressed == 13) {
new_selectimage13.setImageBitmap(bmp);
} else if (buttonpressed == 14) {
new_selectimage14.setImageBitmap(bmp);
} else if (buttonpressed == 15) {
new_selectimage15.setImageBitmap(bmp);
} else if (buttonpressed == 16) {
new_selectimage16.setImageBitmap(bmp);
} else if (buttonpressed == 17) {
new_selectimage17.setImageBitmap(bmp);
} else if (buttonpressed == 18) {
new_selectimage18.setImageBitmap(bmp);
} else if (buttonpressed == 19) {
new_selectimage19.setImageBitmap(bmp);
} else if (buttonpressed == 20) {
new_selectimage20.setImageBitmap(bmp);
}
} catch (Exc