1

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) 

enter image description here

2 Answers2

1

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

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • i do not use the media store one as i had writen my own the problem is it works fine with `String photofile="test"+"jpg";` but if i change it out to `String photofile=editText.getText().toString()+".jpg";` then it crashes – Kewin Björk Nielsen Jul 09 '16 at 12:05
  • 1
    then may be your editext has a null reference have you provided proper id for it? check first of all that @KewinBjörkNielsen String temp = editText.getText().toString().trim() and then use photofile=temp+".jpg"; – TapanHP Jul 09 '16 at 12:11
  • Although i think too, as @Tapanparmar pointed out, it is a null reference in your edittext box: According to your log, the exception occurs in CameraActivity.java at line 31. MAybe that helps a little bit. – Christian Ammann Jul 09 '16 at 12:17
  • what is this guy taken at this line ? i don't understand please let me know @ChristianAmmann File file_image= getDirc(); what is this getDirc(); – TapanHP Jul 09 '16 at 12:22
  • @KewinBjörkNielsen have this resolved your issue ? please let me know – TapanHP Jul 09 '16 at 13:48
  • nope it haven't the line 31 is `EditText editText = (EditText)findViewById(R.id.cardnumberbox);` and Getdirc is the function that create the folder – Kewin Björk Nielsen Jul 09 '16 at 14:16
  • ok gotcha !! but then what do you have at line 31 ? @KewinBjörkNielsen – TapanHP Jul 09 '16 at 14:31
  • is the variable `EditText editText = (EditText)findViewById(R.id.cardnumberbox);` – Kewin Björk Nielsen Jul 09 '16 at 14:32
  • what ? i don't get it sorry what is the exact line 31 ? @KewinBjörkNielsen – TapanHP Jul 09 '16 at 14:34
  • postet a picture with it – Kewin Björk Nielsen Jul 09 '16 at 14:38
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116877/discussion-between-tapan-parmar-and-kewin-bjork-nielsen). – TapanHP Jul 09 '16 at 14:39
  • see my new answer @KewinBjörkNielsen – TapanHP Jul 09 '16 at 15:27
0

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);
    }
}
Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • i had the setcontentview on but becuase i didn't used @injectview to call my edittext then i got the issue since i had injected setcontentview `public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback { @InjectView(R.id.cardnumberbox) EditText cardnumberbox;` this is only the things i had changed it – Kewin Björk Nielsen Jul 09 '16 at 16:12