我还在清单文件中添加了权限。但是该应用一旦启动就会崩溃。这里有什么问题?
MainActivity.java
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView mImageView=(ImageView) findViewById(R.id.img);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button) findViewById(R.id.imgButton);
//
View.OnClickListener imgButtonClickListener=new View.OnClickListener()
{
@Override
public void onClick(View view)
{
dispatchTakePictureIntent();
}
};
//
b1.setOnClickListener(imgButtonClickListener);
}
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");
mImageView.setImageBitmap(imageBitmap);
}
}
}
MainActivity.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.cameraexample.MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="177dp" />
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IMG"
android:id="@+id/imgButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp" />
2017-01-01
Nirvan
+1
什么错误信息(s)你在你的日志? –
+1
如果它崩溃了,会有什么异常呢?始终在问题中包含错误消息和例外情况。 –
+0
有投票结束这个问题。请编辑问题并提供更多关于您所得到异常的数据,并解释您如何尽快编译代码。 –