gpt4 book ai didi

android - 为什么在 startActivityForResult 之后调用 oncreate 方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:15 24 4
gpt4 key购买 nike

在我的应用程序中,我想从 Gallery 中选择图像并在 ListView 中设置图像。在我的 ListView 中,我有 16 行。因此,当项目单击ListView时,打开图库并将图像设置为ListView。但我的问题有时出现在调用 startActivityForResult(),oncreate()` 方法之后。这是点击代码

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

这是onactivityresult

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), 1024, 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 结果。请指导我是什么问题。提前感谢大家..

最佳答案

before the onactivityresult(). But after oncreate() method again startactivity result called

当 Activity 被发送到后台时(当其他 Activity 成为它的顶部时,或者当它被主页按钮发送到后台时)只要系统没有内存压力,它的实例就会保持 Activity 状态。当系统没有足够的内存来执行当前在前台执行的任何操作时,它通常会通过停止和释放内存后台 Activity 来回收内存。

在这种情况下 - 系统会为您提供 Activity.onSaveInstanceState将从将要被杀死的 Activity 中调用的回调,让您有机会在被杀死之前保存任何需要保存的状态。

当您的 Activity 返回前台时 - 它会被重新创建(这就是 onCreate() 再次调用的原因),savedInstanceState 参数不会为空。

savedInstanceState 将包含您在 onSavedInstanceState() 回调中提供的所有额外内容。

理解这一点非常重要。

为了更好的理解,我建议你认真阅读- http://developer.android.com/training/basics/activity-lifecycle/recreating.html

关于android - 为什么在 startActivityForResult 之后调用 oncreate 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26359130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com