0

我有 20 个图像视图,当我单击任何 1 个按钮时,我有 20 个按钮供他们使用从他们那里,让我在我的图像视图上为各个按钮显示这些图像现在问题是,当我执行上述整个过程时,我的活动正在自动重新启动,并且所有首先选择的图像都从他们的图像视图中消失供参考我的代码是如下:

    @Override
         public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           setContentView(R.layout.new_upload);

      // *****************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****************


      // ********************Image 1 button code starts
      // here*******************************
       new_select1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 1;
         dialog.show();
        }
       });

      // ********************Image 1 button code ends
      // here*******************************

      // ********************Image 2 button code starts
      // here*******************************
       new_select2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 2;
         dialog.show();
        }
       });

      // ********************Image 2 button code ends
      // here*******************************

      // ********************Image 3 button code starts
      // here*******************************

       new_select3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 3;
         dialog.show();
        }
       });


      // ********************Image 3 button code ends
      // here*******************************

      // ********************Image 4 button code starts
      // here*******************************
      new_select4.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 4;
         dialog.show();
        }
       });

      // ********************Image 4 button code ends
      // here*******************************

      // ********************Image 5 button code starts
      // here*******************************
     new_select5.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 5;
         dialog.show();
        }
       });

      // ********************Image 5 button code ends
      // here*******************************

      // ********************Image 6 button code starts
      // here*******************************
      new_select6.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 6;
         dialog.show();
        }
       });


      // ********************Image 6 button code ends
      // here*******************************

      // ********************Image 7 button code starts
      // here*******************************
     new_select7.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        buttonpressed = 7;
         dialog.show();
        }
       });

      // ********************Image 7 button code ends
      // here*******************************

      // ********************Image 8 button code starts
      // here*******************************
       new_select8.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 8;
         dialog.show();
        }
       });

      // ********************Image 8 button code ends
      // here*******************************

      // ********************Image 9 button code starts
      // here*******************************
      new_select9.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        buttonpressed = 9;
         dialog.show();
        }
       });

      // ********************Image 9 button code ends
      // here*******************************

      // ********************Image 10 button code starts
      // here*******************************
       new_select10.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        buttonpressed = 10;
         dialog.show();
        }
       });

      // ********************Image 10 button code ends
      // here*******************************

      // ********************Image 11 button code starts
      // here*******************************
       new_select11.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 11;
         dialog.show();
        }
       });

      // ********************Image 11 button code ends
      // here*******************************

      // ********************Image 12 button code starts
      // here*******************************
      new_select12.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 12;
         dialog.show();
        }
       });

      // ********************Image 12 button code ends
      // here*******************************

      // ********************Image 13 button code starts
      // here*******************************
      new_select13.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 13;
         dialog.show();
        }
       });

      // ********************Image 13 button code ends
      // here*******************************

      // ********************Image 14 button code starts
      // here*******************************
      new_select14.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 14;
         dialog.show();
        }
       });

      // ********************Image 14 button code ends
      // here*******************************

      // ********************Image 15 button code starts
      // here*******************************
      new_select15.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 15;
         dialog.show();
        }
       });


      // ********************Image 15 button code ends
      // here*******************************

      // ********************Image 16 button code starts
      // here*******************************
      new_select16.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 16;
         dialog.show();
        }
       });

      // ********************Image 16 button code ends
      // here*******************************

      // ********************Image 17 button code starts
      // here*******************************
     new_select17.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 17;
         dialog.show();
        }
       });


      // ********************Image 17 button code ends
      // here*******************************

      // ********************Image 18 button code starts
      // here*******************************
      new_select18.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 18;
         dialog.show();
        }
       });

      // ********************Image 18 button code ends
      // here*******************************

      // ********************Image 19 button code starts
      // here*******************************
      new_select19.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        buttonpressed = 19;
         dialog.show();
        }
       });


      // ********************Image 19 button code ends
      // here*******************************

      // ********************Image 20 button code starts
      // here*******************************
     new_select20.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         buttonpressed = 20;
         dialog.show();
        }
       });

      // ********************Image 20 button 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 (Exception e)
    {
       Log.e("",e.tostring());
    }

我的日志如下

06-11 12:36:43.899: I/ActivityManager(163): Process com.AUSRWC (pid 18012) has died.
06-11 12:36:43.899: I/ActivityManager(163): Low Memory: No more background processes.
06-11 12:36:43.909: I/WindowManager(163): WIN DEATH: Window{409be3d0 com.AUSRWC/com.AUSRWC.new_upload paused=false}
06-11 12:36:45.199: I/InputReader(163): dispatchTouch::touch event's action is 0
06-11 12:36:45.199: I/InputDispatcher(163): Delivering touch to current input target: action: 0, channel '40866bd8 com.cooliris.media/com.cooliris.media.Gallery (server)'
06-11 12:36:45.279: I/InputReader(163): dispatchTouch::touch event's action is 1
06-11 12:36:45.279: I/InputDispatcher(163): Delivering touch to current input target: action: 1, channel '40866bd8 com.cooliris.media/com.cooliris.media.Gallery (server)'
06-11 12:36:45.279: D/Gallery(18750): performReturn : contentUri = content://media/external/images/media/421
06-11 12:36:45.289: D/Gallery(18750): performReturn : result = Intent { dat=content://media/external/images/media/421 }
06-11 12:36:45.289: I/RenderView(18750): OnPause RenderView com.cooliris.media.RenderView@40515188
06-11 12:36:45.289: E/SensorManager(18750): unregisterListener:: all sensors,  listener = com.cooliris.media.RenderView@40515188
06-11 12:36:45.299: I/GLThread(18750): onPause tid=46
06-11 12:36:45.299: I/Main thread(18750): onPause waiting for mPaused.
06-11 12:36:45.299: I/Main thread(18750): onPause waiting for mPaused.
06-11 12:36:45.309: I/GLThread(18750): mPaused is now true tid=46
06-11 12:36:45.309: I/GLThread(18750): releasing EGL surface because paused tid=46
06-11 12:36:45.309: W/EglHelper(18750): destroySurface()  tid=46
06-11 12:36:45.319: W/EglHelper(18750): finish() tid=46
06-11 12:36:45.349: I/GLThread(18750): releasing EGL context because paused tid=46
06-11 12:36:45.359: I/ActivityManager(163): Start proc com.AUSRWC for activity com.AUSRWC/.new_upload: pid=18810 uid=10067 gids={3003, 1006, 1015}
06-11 12:36:45.419: I/OrientationDebug(163): [pwm] in updateOrientationListenerLp()
06-11 12:36:45.419: V/OrientationDebug(163): in updateOrientationListenerLp(), Screen status=true, current orientation=1, SensorEnabled=true
06-11 12:36:45.419: I/OrientationDebug(163): [pwm] needSensorRunningLp(), return true #4
06-11 12:36:45.439: E/jdwp(18810): Failed sending reply to debugger: Broken pipe
06-11 12:36:45.499: I/GLThread(18750): noticed surfaceView surface lost tid=46
06-11 12:36:45.609: I/ApplicationPackageManager(18810): cscCountry is not German : INU
4

2 回答 2

0

看起来正在重新创建活动。尝试将以下内容添加到清单中的活动条目中

android:configChanges="orientation|keyboardHidden" android:launchMode="singleTask"

于 2012-06-08T10:38:43.617 回答
0

问题可能是由于内存利用率。尝试使用 Memory Analyzer Tool 测试代码,以便找出问题所在。

如果您在活动重新创建期间发布日志语句,我可以为您提供更好的帮助。

于 2012-06-08T11:08:16.523 回答