gpt4 book ai didi

android onActivityResult 不调用

转载 作者:行者123 更新时间:2023-11-29 22:33:40 25 4
gpt4 key购买 nike

我是 Android 的初学者。我有两个屏幕。我在屏幕之间切换,但在我的应用程序中未调用 onActivityResult()。我正在恢复主屏幕。我想要第二个屏幕的结果。

这是我的代码:

mainActivity.java

loadPicture.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

Intent i = new Intent(context, ImageThumbnailsActivity.class);
startActivityForResult(i, 0);
return true;
}

});

ImageThumbnailActivity.java

public class ImageThumbnailsActivity extends Activity {
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
GridView imagegrid;
private int count;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.image_gallery);
init_phone_image_grid();

}

private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapterClass(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
//System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
//System.gc();
Intent intent = new Intent();
intent.putExtra("filename", i);
//startActivity(intent);
setResult(RESULT_OK,intent);
finish();
}
});
}

public class ImageAdapterClass extends BaseAdapter {
private Context mContext;

public ImageAdapterClass(Context c) {
mContext = c;
}

public int getCount() {
return count;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
//System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new GridView.LayoutParams(92, 92));
} else {
i = (ImageView) convertView;
}
return i;
}
}
}

最佳答案

将此添加到您的主要 Activity 中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String filename = data.getStringExtra("filename");
}
}
}

关于android onActivityResult 不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942675/

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