在我的应用程序中,我想从Gallery中选择图像并在ListView中设置图像.在我的ListViewI中有16行.因此,当项目单击inListView打开图库并将图像设置为ListView时.但我的问题有时是afterstartActivityForResult(),oncreate()`方法调用.这是onclick代码
Intent intent = new Intent( Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult( Intent.createChooser(intent,"Select File"),SELECT_PICTURE);
这是一个非常活跃的结果
public void onActivityResult(int requestcode,int resultcode,Intent data) { Log.e("result","result"); displayMetrics = this.getResources().getDisplayMetrics(); Ew = displayMetrics.widthPixels; Eh = displayMetrics.heightPixels; switch (requestcode) { case SELECT_PICTURE: if (resultcode == RESULT_OK) { lelListLayout.setVisibility(View.GONE); relImageLayout.setVisibility(View.VISIBLE); Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); ExifInterface exif = null; // Bitmap bmRotated = null; try { exif = new ExifInterface(selectedImagePath); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED); Log.e("Orientation==>","" + orientation); try { bmRotated = null; bmGallayImage = null; trimCache(); bmGallayImage = convertBitmap(selectedImagePath); bmRotated = InventorySubmitImagesActivity.rotateBitmap( bmGallayImage,orientation); // if(bmRotated.getWidth()>bmRotated.getHeight()){ if (bmRotated.getWidth() > 1024) { float x = 0; x = 1024 / (float) bmRotated.getWidth(); // Log.e("x====","value "+x); bmRotated = Bitmap.createScaledBitmap(bmRotated,1024,(int) (bmRotated.getHeight() * x),true); } /* * }else{ if(bmRotated.getHeight() > 1024){ float x=0; * x=1024/(float)bmRotated.getHeight(); * Log.e("x====","value "+x); * * bmRotated = Bitmap.createScaledBitmap(bmRotated,* (int)(bmRotated.getWidth()*x),true); } } */ Eh = Eh - ll_buttonlayout.getHeight(); float iw = bmRotated.getWidth(); float ih = bmRotated.getHeight(); float diff = Ew / iw; float layoutwidth = Ew; float layoutheight = diff * ih; if (layoutheight > Eh) { diff = Eh / ih; layoutwidth = Ew * diff; layoutheight = Eh; } bmGallayImage = bmRotated; if (bmRotated != null) { RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( (int) layoutwidth,(int) layoutheight); relImage.setLayoutParams(layoutParams); Drawable dr = new BitmapDrawable(bmRotated); old_width = bmRotated.getWidth(); old_height = bmRotated.getHeight(); relImage.setBackgroundDrawable(dr); } left = (int) layoutwidth / 2 - 34; top = (int) layoutheight / 2 - 8; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // drag_check=true; relImage.removeAllViews(); imgMarker = new ImageView(this); final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT); relImage.addView(imgMarker,layoutParams); imgMarker.setScaleType(ImageView.ScaleType.MATRIX); bmdragImage = BitmapFactory.decodeResource(getResources(),R.drawable.image_marker); imgMarker.setImageBitmap(bmdragImage); matrix = new Matrix(); savedMatrix = new Matrix(); oldDist = 0f; start = new PointF(); mid = new PointF(); matrix.postTranslate(left,top); imgMarker.setImageMatrix(matrix); imgMarker.setOnTouchListener(RefurbishmentImageActivity.this); imgMarker.setVisibility(View.VISIBLE); // end.. // } } break;
}
}
在此ListView中选择6或7个图像后调用oncreate方法,在onactivityresult()之前.但是在oncreate()方法之后再次调用startactivity结果.请指导我是什么问题.谢谢InAdvance给所有人..
解决方法
before the onactivityresult(). But after oncreate() method again startactivity result called
当Activity被发送到后台时(当其他活动在其上面时,或者当它被主页按钮发送到后台时),只要系统没有内存压力,它的实例就会保持活动状态.当系统没有足够的内存来处理前台正在做的任何事情时,它通常会通过停止和释放内存后台活动来重新声明内存.
在这种情况下 – 系统为您提供Activity.onSaveInstanceState
回调,该回调将从将要被杀死的活动中调用,以便您有机会保存在被杀死之前需要保存的任何状态.
当你的活动将返回前景时 – 它将被重新创建(这就是为什么onCreate()再次调用),其中savedInstanceState参数不会为null.
savedInstanceState将保存包含您在onSavedInstanceState()回调中提供的所有额外内容的包.
这一点非常重要.
为了更好地理解,我建议你认真阅读 – http://developer.android.com/training/basics/activity-lifecycle/recreating.html