【发布时间】:2015-08-10 22:42:23
【问题描述】:
在我的应用程序中,我有两个按钮,一个用于从画廊(从设备)加载图像,另一个用于通过访问设备的相机拍照,我的代码在某些设备上正常工作,bt 在某些设备上,单击图库中的图像时应用程序崩溃。有人可以帮我找出实际的问题吗??
public class MainActivity extends ActionBarActivity {
private static int RESULT_LOAD_IMAGE = 1;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
this.imageView = (ImageView)this.findViewById(R.id.imgView);
Button photoButton = (Button) this.findViewById(R.id.button2);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
而xml是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></ImageView>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<Button android:id="@+id/buttonLoadPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Load Picture"
android:layout_gravity="center"></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:layout_alignParentRight="true"/>
</RelativeLayout>
【问题讨论】:
-
您应该为异常提供堆栈跟踪。布局代码也没有帮助。
-
你添加了 android.permission.READ_EXTERNAL_STORAGE 吗?
标签: android android-layout android-intent android-activity