【问题标题】:After photo is taken, OnActivityResult is not called拍照后,不调用 OnActivityResult
【发布时间】:2012-11-09 20:21:44
【问题描述】:

我正在尝试在拍摄后获取照片,但未调用 onActivityResult:

public class questionListView extends LinearLayout {

protected static final Integer PICTURE_RESULT = 1;
private Context ctx;

public Uri path;

public questionListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    ctx = context;
    init();
}

public questionListView(Context context) {
    super(context);
    ctx = context;
    init();
}

private void init() {

    LayoutInflater factory = LayoutInflater.from(getContext());
    View myView = factory.inflate(R.layout.view1, null);

    LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    myView.setLayoutParams(params);

    addView(myView);

    Button btaddphoto = (Button) myView.findViewById(R.id.btAddPhoto);

    btaddphoto.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String _path = Environment.getDataDirectory() + File.separator + "pic1.png";
            File file = new File(_path);
            Uri outputFileUri = Uri.fromFile(file);

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            ((Activity) ctx).startActivityForResult(intent, PICTURE_RESULT);

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap mBitmap;
    String _path = Environment.getDataDirectory() + File.separator + "pic1.png";
    mBitmap = BitmapFactory.decodeFile(_path);
    if (mBitmap == null) {
        // bitmap still null
    } else {

        // kleine bitmap maken
        // deze in een imageview toevoegen aan layout lvPhotos

    }
}

}

我在一行中遇到错误

super.OnActivityResult(requestCode, resultCode, data);

错误信息:

The method onActivityResult(int, int, Intent) is undefined for the type LinearLayout

如果我离开了,函数 onActivityResult 不会被调用。

我做错了什么?

rg, 埃里克

【问题讨论】:

  • onActivityResult 应该在您的活动中!

标签: android android-layout android-intent android-widget


【解决方案1】:

您正在从LinearLayout 类调用onActivityResult()onActivityResult() 方法特定于Activity

更多详情请查看处理该主题的官方Android developers site

附带说明,最佳实践是从 XML 而不是从 Java 代码创建布局。它不仅编码速度更快,而且在性能方面也快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2015-12-28
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多