首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >相机应用程序开发和倾斜设备时的错误

相机应用程序开发和倾斜设备时的错误
EN

Stack Overflow用户
提问于 2014-05-02 15:33:31
回答 1查看 117关注 0票数 0

我遵循了一个教程https://www.youtube.com/watch?v=k-3zXb7GteU并创建了我的相机应用程序,当我点击按钮时,我会让相机拍照。我有两个问题,

第一,当相机进来拍照时,如果我倾斜手机拍照,我的应用程序就会停止工作。在水平拍摄的意义上倾斜暴徒。

我想将这张照片连接到特定的数据库数据,比如对于特定的id,只存储这些照片。

我将我的代码粘贴在这里,请帮助我,我是新手。

mainActivity.java文件

`

代码语言:javascript
复制
private static String logCat= "Camera App";
private static int TAKE_PICTURE=1;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    Button cameraButton = (Button) findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);

}
private OnClickListener cameraListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        takePhoto(v);
    }
};

private void takePhoto(View v){
    Intent intent =  new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);

}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode==Activity.RESULT_OK){
        Uri selectedImg = imageUri;
        getContentResolver().notifyChange(selectedImg, null);

        ImageView imageView = (ImageView) findViewById(R.id.image_id);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        try{

            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImg);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(MainActivity.this, selectedImg.toString(), Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Log.e(logCat, e.toString());
        }
    }
}`

activity_main.xml文件

`

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     tools:context="com.example.camera.MainActivity" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<ImageView
    android:id="@+id/image_id"
    android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/image_cd_camera" />

 <Button
    android:id="@+id/button_camera"
    android:layout_width="132dp"
    android:layout_height="wrap_content"
    android:text="@string/button_camera_t" />

 </LinearLayout>`

错误日志

`

代码语言:javascript
复制
05-02 13:42:28.378: E/AndroidRuntime(328): FATAL EXCEPTION: main  
05-02 13:42:28.378: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to resume   activity {com.example.camera/com.example.camera.MainActivity}: 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1,   result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/picture.jpg typ=image/jpeg (has extras) }} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException

05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:258
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2609)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3518)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.access$700(ActivityThread.java:133)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Looper.loop(Looper.java:137)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.main(ActivityThread.java:4803)
05-02 13:42:28.378: E/AndroidRuntime(328):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 13:42:28.378: E/AndroidRuntime(328):  at java.lang.reflect.Method.invoke(Method.java:511)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-02 13:42:28.378: E/AndroidRuntime(328):  at dalvik.system.NativeStart.main(Native Method)
05-02 13:42:28.378: E/AndroidRuntime(328): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/picture.jpg typ=image/jpeg (has extras) }} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3147)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2568)
05-02 13:42:28.378: E/AndroidRuntime(328):  ... 13 more
05-02 13:42:28.378: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Parcel.readException(Parcel.java:1431)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.os.Parcel.readException(Parcel.java:1379)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:452)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.ContentResolver.notifyChange(ContentResolver.java:1278)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.content.ContentResolver.notifyChange(ContentResolver.java:1257)
05-02 13:42:28.378: E/AndroidRuntime(328):  at com.example.camera.MainActivity.onActivityResult(MainActivity.java:69)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.Activity.dispatchActivityResult(Activity.java:5192)
05-02 13:42:28.378: E/AndroidRuntime(328):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3143)
05-02 13:42:28.378: E/AndroidRuntime(328):  ... 14 more`
EN

回答 1

Stack Overflow用户

发布于 2014-05-02 17:08:58

将这些指定到您的清单中...

代码语言:javascript
复制
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />

并在manifest的main activity标签中尝试...android:configChanges="orientation|screenSize|keyboardHidden“

像这样..。

代码语言:javascript
复制
<activity 
        android:name=".MainActivity" 
        android:label="@string/app_name" 
        android:configChanges="orientation|screenSize|keyboardHidden" > 
        <intent-filter> 
            <action android:name="android.intent.action.MAIN" /> 
            <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23423055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档