android,,onActivityResult的作用是啥啊,requestCode, resultCode是啥意思啊?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android,,onActivityResult的作用是啥啊,requestCode, resultCode是啥意思啊?相关的知识,希望对你有一定的参考价值。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0x11 && resultCode==0x11) //判断是否为待处理的结果
Bundle bundle=data.getExtras(); //获取传递的数据包
String city=bundle.getString("city"); //获取选择的头像ID
TextView tv=(TextView)findViewById(R.id.city); //获取布局文件中添加的ImageView组件
tv.setText(city);
你可以了解一下下面两个方法,就能明白了:
startActivityForResult(Intent intent, Int requestCode);
setResut(int resultCode, Intent intent);
简单的说requestCode,resultCode是用来识别区分是哪个activity返回的处理结果。本回答被提问者和网友采纳
Android - 无法解析方法'onActivityResult(int,int,android.content.Intent)
【中文标题】Android - 无法解析方法\'onActivityResult(int,int,android.content.Intent)【英文标题】:Android - Cannot resolve method 'onActivityResult(int, int, android.content.Intent)Android - 无法解析方法'onActivityResult(int,int,android.content.Intent) 【发布时间】:2016-05-08 13:10:20 【问题描述】:我需要设置/添加照片/电影作为 ToDo 应用的附件。 不幸的是: onActivityResult 行 super.onActivityResult(requestCode, resultCode, data);以红色突出显示,我收到错误消息:
“无法解析方法'onActivityResult(int, int, android.content.Intent)'”
AddActivity.java
Button button_z = (Button) findViewById(R.id.button_z);
button_z.setonclickListener(new View.onclickListener()
@Override
public void onclick(View view2)
selectImage();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
if (requestCode == REQUEST_CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
90, bytes);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
imageView.setImageBitmap(thumbnail);
else if (requestCode == SELECT_FILE)
Uri selectedImageUri = data.getData();
String[] projection = MediaStore.MediaColumns.DATA;
CursorLoader cursorLoader = new CursorLoader(AddActivity.this, selectedImageUri, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
imageView.setImageBitmap(bm);
private void selectImage()
final CharSequence[] items = "Zrób zdjęcie lub nagranie", "Wybierz istniejące", "Anuluj" ;
AlertDialog.Builder builder = new AlertDialog.Builder(AddActivity.this);
builder.setTitle("Dodaj załącznik");
builder.setItems(items, new DialogInterface.onclickListener()
@Override
public void onclick(DialogInterface dialog, int item)
if (items[item].equals("Zrób zdjęcie lub nagranie"))
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
else if (items[item].equals("Wybierz istniejące"))
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Wybierz plik"),
SELECT_FILE);
else if (items[item].equals("Anuluj"))
dialog.dismiss();
);
builder.show();
【问题讨论】:
【参考方案1】:您的代码中缺少);
。
Button button_z = (Button) findViewById(R.id.button_z);
button_z.setonclickListener(new View.onclickListener()
@Override
public void onclick(View view2)
selectImage();
); //here
【讨论】:
以上是关于android,,onActivityResult的作用是啥啊,requestCode, resultCode是啥意思啊?的主要内容,如果未能解决你的问题,请参考以下文章
Android基础:startActivityForResult 和 onActivityResult 问题
android中onActivityResult的用途是啥[重复]
Android - 片段的 onActivityResult() 中的 NPE