Reputation: 679
i trying to get the edit text to name the picture that had been captured but it crash when i try are there anyway to do that? it works perfect if i just do
String photofile="test"+"jpg";
but not with edit text
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {
EditText editText = (EditText)findViewById(R.id.cardnumberbox);
File file_image= getDirc();
if (!file_image.exists() && !file_image.mkdirs()){
Toast.makeText(getApplicationContext(),"Kan ikke lave mappe til at gemme billederne",Toast.LENGTH_SHORT).show();
return;
}
String photofile=editText.getText().toString()+".jpg";
String file_name=file_image.getAbsolutePath()+"/" +photofile;
File picfile=new File(file_name);
try {
outputStream=new FileOutputStream(picfile);
outputStream.write(bytes);
outputStream.close();
} catch (FileNotFoundException e){}
catch (IOException ex) {}
finally {
}
Toast.makeText(getApplicationContext(),"Bilederne er gemt",Toast.LENGTH_SHORT).show();
refreshcamera();
refreshgallery(picfile);
}
};
}
private File getDirc(){
File folder= new File("sdcard");
if (!folder.exists()) {
folder.mkdir();
}
return new File(folder,"pics");
}
07-09 13:14:07.478 3128-3128/camapp.camapp E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{camapp.camapp/camapp.camapp.CameraActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2021) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) at android.app.ActivityThread.access$600(ActivityThread.java:140) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4895) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:72) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183) at camapp.camapp.CameraActivity.(CameraActivity.java:31) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) at android.app.ActivityThread.access$600(ActivityThread.java:140) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4895) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 236
Reputation: 6201
For understanding of a common mistake that @kewin made that he declared and tries to access EditText
before onCreate()
method i am posting this answer for his understanding.
This is common mistakes that beginners makes,so for concept understanding read it,
First thing first how xml and java relates each other is using R file and see carefully this line setContentView(R.layout.Your_XML_FileName);
you are accessing your layout xml file in java after this line only your xml components are available for further procedure.
public class Your_Class_Name extends Activity {
//declaration is here but you can not access because it is still not available in java side
private EditText myEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this is place where you connect all your xml content in java
setContentView(R.layout.Your_XML_FileName);
...
//now you can access everything from xml side suppose and EditText with id edittext
myEdit = (EditText)findViewById(R.id.edittext);
}
}
Upvotes: 0
Reputation: 6201
Intent to call to capture photo
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
you will get image in your onActivityResult()
method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}
Then Write that Into Internal Memory
String photofile=editText.getText().toString().trim()+".jpg";
// The openfileOutput() method creates a file on the phone/internal storage in the context of your application
final FileOutputStream fos = openFileOutput(photofile, Context.MODE_PRIVATE);
// Use the compress method on the BitMap object to write image to the OutputStream
bm.compress(CompressFormat.JPEG, 90, fos);
or use this to save on external storage
from developer docs
File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File file = new File(path, editText.getText().toString().trim()+".jpg");
To Read it use
Bitmap bitmap = BitmapFactory.decodeFile(file);
Also i suggest you to read this answer for full code.
and the cause for null pointer exception is explain in these answers that can be a mistake in your code see these answers
https://stackoverflow.com/a/11470571/5476209
https://stackoverflow.com/a/22476300/5476209
Upvotes: 1