0

データベースの名前でカメラを起動したいのですが、データベースが3つあります。次のようなコードがあります。

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

The exception says that one of the arguments to the File constructor is null. It can't be path_ because that would have led to a previous exception. It must be filename_. Your three-way if-else allows for the possibility that none of the branches will be true, and if none were, then filename_ would be null -- so that must be what's happening. Can you have a default filename if none of the conditions are true -- i.e., can you add an else without an if, at the end of the chain?

于 2012-04-24T03:02:45.767 に答える