0

我想用数据库中的名称启动相机,我有三个数据库..我有这样的代码:

public void startCamera() 
{
    long tim=System.currentTimeMillis();
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
     String curTime =df.format(tim);
     System.out.println("Time : " + curTime);

    Cursor c = helper.getKomp(almagId2);
    Cursor ca = helper.getSat(almagId);
    Cursor cb = helper.getUlok(almagId1);
    if(c.moveToFirst()){
        fileName =c.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }else if(ca.moveToFirst()){
        fileName =ca.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }else if(cb.moveToFirst()){
        fileName =cb.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }

    _path=Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";


    file = new File(_path, fileName);
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }               
    System.out.println(file);
    Uri outputFileUri = Uri.fromFile(file);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, IMAGE_CAPTURE);

    SQLiteDatabase db = helper.getWritableDatabase();
    if(c.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+c.getString(1)+"','"+file+"','"+curTime+"')");
    }else if(ca.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+ca.getString(1)+"','"+file+"','"+curTime+"')");
    }else if(cb.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+cb.getString(1)+"','"+file+"','"+curTime+"')");
    }
    db.close();


}

但是当我想调用方法 startCamera 我得到这样的错误:

04-24 09:29:34.902: ERROR/AndroidRuntime(9411): FATAL EXCEPTION: main
04-24 09:29:34.902: ERROR/AndroidRuntime(9411): java.lang.NullPointerException
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.io.File.<init>(File.java:168)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.sat.alfaloc.Camera.startCamera1(Camera.java:153)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.sat.alfaloc.Camera.onClick(Camera.java:319)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.view.View.performClick(View.java:2408)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.view.View$PerformClick.run(View.java:8816)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Handler.handleCallback(Handler.java:587)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Looper.loop(Looper.java:123)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.lang.reflect.Method.invoke(Method.java:521)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at dalvik.system.NativeStart.main(Native Method)

我怎么能解决这个问题?谢谢你的反馈:)

上面的代码编辑如下:

public void startCamera1() 
{
    long tim=System.currentTimeMillis();
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
     String curTime =df.format(tim);
     System.out.println("Time : " + curTime);
     SQLiteDatabase db = helper.getWritableDatabase();

    Cursor c = helper.getKomp(almagId2);
    Cursor ca = helper.getSat(almagId);
    Cursor cb = helper.getUlok(almagId1);
    if(c.moveToFirst()){
        fileName =c.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        _path=Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+c.getString(1)+"','"+file+"','"+curTime+"')");
    }
    if(ca.moveToFirst()){
        fileName =ca.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+ca.getString(1)+"','"+file+"','"+curTime+"')");
    }
    if(cb.moveToFirst()){
        fileName =cb.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+cb.getString(1)+"','"+file+"','"+curTime+"')");
    }

    db.close();

}

这没有错误,但活动调用不起作用..如何解决它?谢谢你:)

4

1 回答 1

0

异常表示File构造函数的参数之一为空。不可能,path_因为那会导致先前的异常。一定是filename_。您的三向 if-else 允许没有任何分支为真,如果没有,则为filename_空 - 所以这一定是正在发生的事情。如果没有一个条件为真,你能有一个默认文件名吗?即,你可以在链的末尾添加一个else不带, 的文件名吗?if

于 2012-04-24T03:02:45.767 回答