【问题标题】:Android doesn't return to activity after taking photo拍照后 Android 无法恢复活动
【发布时间】:2017-07-19 19:58:44
【问题描述】:

我正在尝试创建一个可以拍照和查看缩略图的应用。

我已按照https://developer.android.com/training/camera/photobasics.html 的教程进行操作 但没有运气。

问题是我的应用程序不会返回到相机开始的活动。 相反,它返回到我的 MainActivity 并打开导航抽屉..

这是我的相机活动:

public class CameraTestActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_test);
        //Haal de toolbar op
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        //zet de toolbar als action bar
        setSupportActionBar(toolbar);
        //zet de homebutton
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Button photoButton = (Button) findViewById(R.id.photoButton);
        photoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();

            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            ImageView bitmap = (ImageView) findViewById(R.id.bitmap);
            bitmap.setImageBitmap(imageBitmap);
        }
    }
}

XML 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ext="http://schemas.android.com/apk/res-auto"
    android:id="@+id/testers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="be.kdg.integratieproject.fragments.FragmentMain">

    <TextView
        android:id="@+id/feedback_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@color/lightteal"
        android:text="Webview"
        android:fontFamily="sans-serif-medium"
        android:textSize="35sp"
        android:paddingTop="19dp"
        android:textAlignment="center"
        android:height="80dp"
        android:textColor="@color/lightgray" />

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar_transparant" />

    <Button
        android:id="@+id/photoButton"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:backgroundTint="@color/darkorange"
        android:layout_below="@id/feedback_title"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="foto nemen"
        android:textStyle="bold"
        android:textColor="@color/white"/>

    <ImageView
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:layout_below="@id/photoButton"
        android:id="@+id/bitmap"
        />

</RelativeLayout>

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    我找到了解决办法..

    原因是onActivityResult没有被调用。

    删除android清单中活动的noHistory="true"

    缩略图现在可以完美运行了!

    来源:onActivityResult method not being called Android

    【讨论】:

      【解决方案2】:

      尝试像这样更改函数dispatchTakePictureIntent()

      private void dispatchTakePictureIntent() {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多