1

我正在制作一个 android 应用程序,它从相机中获取图像然后显示它。但是,我无法显示单击的图像可能是因为onActivityResult()未触发。

这是我的一段代码。谁能建议我我错过了什么?

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final int CAMERA_PIC_REQUEST = 1337; 
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
            @override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                Log.d("Message1", "I reached 2");
                 //super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == CAMERA_PIC_REQUEST) {
                    // do something
                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
                    ImageView image = (ImageView) findViewById(R.id.imageView1);  
                    image.setImageBitmap(thumbnail); 

                }
            }




        });
    }
} 
4

1 回答 1

1

onActivityResult() 必须在您的 Activity 类中声明(而不是在 onClickListener 中)。如果你更正了“@override”('o' 必须大写),在你当前的 onActivityResult() 声明之前的错字,你会明白我的意思......

请参阅Activity.onActivityResult() 文档

这是您的课程的外观:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final int CAMERA_PIC_REQUEST = 1337; 
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       Log.d("Message1", "I reached 2");
       //super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == CAMERA_PIC_REQUEST) {
         // do something
         Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
         ImageView image = (ImageView) findViewById(R.id.imageView1);  
         image.setImageBitmap(thumbnail); 
       }
     }

} 
于 2012-04-09T03:30:46.877 回答