startActivityForResult() 不调用 onActivityResult(int requestCode, int resultCode, Intent data)?

Posted

技术标签:

【中文标题】startActivityForResult() 不调用 onActivityResult(int requestCode, int resultCode, Intent data)?【英文标题】:startActivityForResult() dont call to onActivityResult(int requestCode, int resultCode, Intent data)? 【发布时间】:2012-06-07 17:03:52 【问题描述】:

我学习安卓,我是初学者, 我试图得到一张照片并将其转换为我的图像视图, 但在调试模式下,我看到该应用程序执行该行: startActivityForResult(i,camdata); 带我到 cam 窗口,在我得到照片后,它要求我保存照片,然后我保存它。 在那之后发生了-我的意思是我没有到达onActivityResult(int requestCode,int resultCode,Intent data)这一行。 为什么? 代码:

package com.thenewboston.elyakim;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Cam extends Activity implements OnClickListener

    ImageButton ib;
    Button b;
    ImageView iv;
    Intent i;
    final static int camdata = 0;
    Bitmap bmp ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        initialize();
        InputStream is1 = getResources().openRawResource(R.drawable.ic_launcher);
        bmp = BitmapFactory.decodeStream(is1);

    
    private void initialize() 
        // TODO Auto-generated method stub
        iv = (ImageView) findViewById(R.id.imageView1);
        ib = (ImageButton) findViewById(R.id.imageButton1);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);
        ib.setOnClickListener(this);
    
    public void onClick(View arg0) 
        // TODO Auto-generated method stub
        switch (arg0.getId())
        case R.id.button1:
            try 
                getApplicationContext().setWallpaper(bmp);
             catch (IOException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            
            break;

        case R.id.imageButton1:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i,camdata);
            break;

        

        
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==RESULT_OK)
            Bundle extras = data.getExtras();
            bmp  = (Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);
        
        
    

【问题讨论】:

【参考方案1】:
if(requestCode==RESULT_OK)
        Bundle extras = data.getExtras();
        bmp  = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    

必须

if(requestCode==camdata)
    if(resultCode==RESULT_OK)
        iv.setImageUri(Uri.parse(data.getDataString()));
    

上面在imageview中显示选中的图片

【讨论】:

以上是关于startActivityForResult() 不调用 onActivityResult(int requestCode, int resultCode, Intent data)?的主要内容,如果未能解决你的问题,请参考以下文章

Fragment的startActivityForResult和Activity的startActivityForResult的区别

如何在 onActivityResult 中调用 startActivityForResult?

startActivityForResult和setResult详解

Android------startActivityForResult

startActivityForResult详解

如何从 TabHost Activity 返回结果(startActivityForResult)?