应用程序在更改方向时崩溃

时间:2013-04-26 05:36:09

标签: android

其实我是android的新手,我正在制作一个小型的Android应用程序,我可以捕获一张照片并将其上传到网络服务器,我找到了一些示例代码来实现它。

问题在于,当我启动应用程序时,它在横向或纵向上都很有效,但如果我将方向从横向更改为纵向或从纵向更改为横向,则会崩溃。

以下是Eclipse的日志:

04-26 13:04:36.380: I/dalvikvm(887): threadid=3: reacting to signal 
3 04-26 13:04:36.510: I/dalvikvm(887): Wrote stack traces to '/data/anr/traces.txt' 
04-26 13:04:36.880: I/dalvikvm(887): threadid=3: reacting to signal 3 
04-26 13:04:36.890: I/dalvikvm(887): Wrote stack traces to '/data/anr/traces.txt' 
04-26 13:04:37.180: D/gralloc_goldfish(887): Emulator without GPU emulation detected. 
04-26 13:04:37.390: I/dalvikvm(887): threadid=3: reacting to signal 3 
04-26 13:04:37.530: I/dalvikvm(887): Wrote stack traces to '/data/anr/traces.txt' 
04-26 13:07:26.370: D/HEE(887): /mnt/sdcard/neosoft/ocr/photo/IMG_20130426_130726.jpg 
04-26 13:07:27.840: D/dalvikvm(887): GC_FOR_ALLOC freed 128K, 3% free 9168K/9415K, paused 404ms 
04-26 13:07:44.929: D/AndroidRuntime(887): Shutting down VM 
04-26 13:07:44.929: W/dalvikvm(887): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
04-26 13:07:44.990: E/AndroidRuntime(887): FATAL EXCEPTION: main 
04-26 13:07:44.990: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atakephoto1/com.example.atakephoto1.Photo1Activity}: java.lang.NullPointerException 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.access$700(ActivityThread.java:123) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.os.Looper.loop(Looper.java:137) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at dalvik.system.NativeStart.main(Native Method) 
04-26 13:07:44.990: E/AndroidRuntime(887): Caused by: java.lang.NullPointerException 
04-26 13:07:44.990: E/AndroidRuntime(887):  at com.example.atakephoto1.Photo1Activity.onCreate(Photo1Activity.java:139) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.Activity.performCreate(Activity.java:4465) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
04-26 13:07:44.990: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
04-26 13:07:44.990: E/AndroidRuntime(887):  ... 12 more 04-26 13:07:45.751: I/dalvikvm(887): threadid=3: reacting to signal 3 
04-26 13:07:45.760: I/dalvikvm(887): Wrote stack traces to '/data/anr/traces.txt'

这是我的java代码:

package com.example.atakephoto1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Photo1Activity extends Activity {
//private AutoFocusCallback myAutoFocusCallback = null;
public static String image_file_path;
public static String php_upload_url;
public static String photoPath;
String TAG="tag";
private Camera mCamera;
private CameraPreview mPreview;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
SQLiteDatabase db;
public String db_name = "config";
public String table_name = "config";
NewListDataSQL helper = new NewListDataSQL(Photo1Activity.this, db_name);
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
      public void onAutoFocus(boolean success, Camera camera) {

      }
};


public static boolean exists(String URLName){
    try {
      HttpURLConnection.setFollowRedirects(false);
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestMethod("HEAD");

      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
}

protected void alertbox(String title, String mymessage) {
    new AlertDialog.Builder(this)
       .setMessage(mymessage)
       .setTitle(title)
       .setCancelable(true)
       .setNeutralButton(android.R.string.cancel,
          new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton){}
          })
       .show();
}

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // some work that needs to be done on orientation change
}

@Override
public void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo1);

    db = helper.getReadableDatabase();

    Cursor cursor = db.rawQuery("select value from config where name='image_save_path'", null);
    int rows_num = cursor.getCount();
    if(rows_num != 0) {
     cursor.moveToFirst(); 
     for(int i=0; i<rows_num; i++) {
      String strCr = cursor.getString(0);
      photoPath = strCr;
      cursor.moveToNext();
     }
    }
    cursor.close();
    Cursor cursor_php = db.rawQuery("select value from config where name='php_upload_url'", null);

    int rows_num_php = cursor_php.getCount();
    if(rows_num_php != 0) {
        cursor_php.moveToFirst(); 
     for(int i=0; i<rows_num_php; i++) {
      String strCr = cursor_php.getString(0);
      php_upload_url = strCr;
      cursor_php.moveToNext();
     }
    }
    cursor_php.close(); 

    // Create an instance of Camera
    mCamera = getCameraInstance();
    //final Camera.Parameters parameters = mCamera.getParameters();
    Parameters parameters = mCamera.getParameters();
    List<Size> sizes = parameters.getSupportedPictureSizes();
    Camera.Size size = sizes.get(0);
    for(int i=0;i<sizes.size();i++)
    {
        if(sizes.get(i).width > size.width)
            size = sizes.get(i);
    }
    parameters.setPictureSize(size.width, size.height);
    parameters.setPictureFormat(ImageFormat.JPEG);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    parameters.setSceneMode(Parameters.SCENE_MODE_AUTO);
    parameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
    parameters.setExposureCompensation(0);
    parameters.setPictureFormat(ImageFormat.JPEG);
    parameters.setJpegQuality(100);



    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Parameters.FOCUS_MODE_AUTO)){
        parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO); 
    }

    mCamera.setParameters(parameters);

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);    


    final FrameLayout frame01 = (FrameLayout) findViewById(R.id.camera_preview);
    frame01.setOnClickListener(new View.OnClickListener() {         
        public void onClick(View v) {
            mCamera.autoFocus(myAutoFocusCallback);
        }
    });




 // Add a listener to the Capture button
    final Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
            public void onClick(View v) {
                // get an image from the camera
                captureButton.setEnabled(false);
                mCamera.autoFocus(myAutoFocusCallback);
                mCamera.takePicture(null, null, mPicture);

            }
        }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_photo1, menu);
    return true;
}

private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}    

private PictureCallback mPicture = new PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: ");
                //e.getMessage());
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            image_file_path = pictureFile.getPath();
            Log.d("HEE",pictureFile.getPath());
            FileUploadTask fileuploadtask = new FileUploadTask();  
            fileuploadtask.execute();
            mCamera.startPreview();
            final Button captureButton = (Button) findViewById(R.id.button_capture);
            captureButton.setEnabled(true);


        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File mediaStorageDir = new File(filepath, photoPath );
    if(!mediaStorageDir.exists()){
        Log.d("HERE",photoPath );
        try
        {
            mediaStorageDir.mkdirs();
            Log.d("HERE","after");
        } catch (Exception e) {
            Log.d("HERE",e.getMessage().toString());
        }

    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

 class FileUploadTask extends AsyncTask<Object, Integer, Void> {

        private ProgressDialog dialog = null;  
        HttpURLConnection connection = null;  
        DataOutputStream outputStream = null;  
        DataInputStream inputStream = null;  
        String pathToOurFile = image_file_path;
        String urlServer = php_upload_url;
        String lineEnd = "\r\n";  
        String twoHyphens = "--";  
        String boundary = "*****";  

        File uploadFile = new File(pathToOurFile);  
        long totalSize = uploadFile.length(); // Get size of file, bytes  

        @Override  
        protected void onPreExecute() {  
            dialog = new ProgressDialog(Photo1Activity.this);  
            dialog.setMessage("Uploading...");  
            dialog.setIndeterminate(false);  
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
            dialog.setProgress(0);  
            dialog.show();  
        }  

        @Override  
        protected Void doInBackground(Object... arg0) {  

            long length = 0;  
            int progress;  
            int bytesRead, bytesAvailable, bufferSize;  
            byte[] buffer;  
            int maxBufferSize = 256 * 1024;

            try {  
                FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));  

                URL url = new URL(urlServer);  
                connection = (HttpURLConnection) url.openConnection();  

                // Set size of every block for post  
                connection.setChunkedStreamingMode(256 * 1024);// 256KB  

                // Allow Inputs & Outputs  
                connection.setDoInput(true);  
                connection.setDoOutput(true);  
                connection.setUseCaches(false);  

                // Enable POST method  
                connection.setRequestMethod("POST");  
                connection.setRequestProperty("Connection", "Keep-Alive");  
                connection.setRequestProperty("Charset", "UTF-8");  
                connection.setRequestProperty("Content-Type",  
                "multipart/form-data;boundary=" + boundary);  

                outputStream = new DataOutputStream(  
                connection.getOutputStream());  
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);  
                outputStream  
                        .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""  
                                + pathToOurFile + "\"" + lineEnd);  
                outputStream.writeBytes(lineEnd);  

                bytesAvailable = fileInputStream.available();  
                bufferSize = Math.min(bytesAvailable, maxBufferSize);  
                buffer = new byte[bufferSize];  

                // Read file  
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

                while (bytesRead > 0) {  
                    outputStream.write(buffer, 0, bufferSize);  
                    length += bufferSize;  
                    progress = (int) ((length * 100) / totalSize);  
                    publishProgress(progress);  

                    bytesAvailable = fileInputStream.available();  
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);  
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);  
                }  
                outputStream.writeBytes(lineEnd);  
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens  
                        + lineEnd);  
                publishProgress(100);  

                // Responses from the server (code and message)  
                // int serverResponseCode = connection.getResponseCode();  
                // String serverResponseMessage = connection.getResponseMessage();  

                fileInputStream.close();  
                outputStream.flush();  
                outputStream.close();  

            } catch (Exception ex) {  

            }  
            return null;  
        }  

        @Override  
        protected void onProgressUpdate(Integer... progress) {  
            dialog.setProgress(progress[0]);  
        }  

        @Override  
        protected void onPostExecute(Void result) {  
            try {  
                dialog.dismiss();  
                // TODO Auto-generated method stub  
            } catch (Exception e) {  
            }  
        }  

    } 

}

4 个答案:

答案 0 :(得分:1)

尝试使用

  

&LT;活动名称=&#34; .YourActivity&#34;   机器人:configChanges =&#34;取向|屏幕尺寸&#34; /&GT;

清单文件中的

通过声明这个android将管理自己的活动状态。可能它会停止崩溃活动。

答案 1 :(得分:1)

将以下内容添加到清单中的活动中。

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize">
</activity>

在YourActivity.java中

附加以下代码:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

如果这仍然不起作用,请告诉我。

答案 2 :(得分:0)

尝试在活动的清单文件中添加以下属性

android:configChanges="keyboardHidden|orientation"

答案 3 :(得分:0)

确保调用Camera.release()以在onPause()中释放相机 并使用Camera.reconnect()

在onResume()中重新打开相机