i want to start camera with name from database and i have three database..
i have code like this:
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();
}
but when i want to call method startCamera i get error like this:
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)
how i can solved this??thank you for feed back 🙂
code above edited like this:
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();
}
this is no error but activity call doesn’t work..how solve it??thank you 🙂
The exception says that one of the arguments to the
Fileconstructor is null. It can’t bepath_because that would have led to a previous exception. It must befilename_. Your three-way if-else allows for the possibility that none of the branches will be true, and if none were, thenfilename_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 anelsewithout anif, at the end of the chain?