0

I would like to ask if there is any way to convert image view image (the image is set from gallery / camera capture) to a base64 string? I have tried multiple ways but i have been getting null pointer exception. Does anyone have any ideas on fixing this? I am storing my base64 string into windows azure. I have tried putting this method into the. Please note that i removed my api key and the web url for the initmobileservice method so please understand and thank you in advance

ImageView image;
Bitmap bitmap;
String baseUrl = "";
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addramps);

    GetLatLon();
    InitMobileService();
    pref = getApplicationContext().getSharedPreferences("Account", 0);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    description = (EditText) findViewById(R.id.tb_Description);
    rbStairs = (RadioButton) findViewById(R.id.stairsRadioButton);
    rbRamps = (RadioButton) findViewById(R.id.rampssRadioButton);
private void dataSave() {
    if (rbStairs.isChecked() || rbRamps.isChecked()) {

        //Error Here

        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.id.stairsRampsPhoto);
        ByteArrayOutputStream stream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
        byte[] image=stream.toByteArray();
        final String base64 = Base64.encodeToString(image, 0);

        final String lon = latlon.get(0), lat = latlon.get(1);

        String userType = GetData("userType");
        final String id = GetData("id");
        if (userType.equals("caretaker")) {
            mClient.getTable(careTakersProfile.class).where().field("id")
                    .eq(id)
                    .execute(new TableQueryCallback<careTakersProfile>() {
                        @Override
                        public void onCompleted(
                                List<careTakersProfile> result, int arg1,
                                Exception exception,
                                ServiceFilterResponse response) {
                            if (exception == null) {
                                mClient.getTable(dataAnalysis.class)
                                        .insert(new dataAnalysis(
                                                base64,
                                                description.getText()
                                                .toString(),
                                                lat,
                                                lon,
                                                result.get(0).getNRIC(),
                                                rbStairs.isChecked() ? "Stairs"
                                                        : "Ramps"),
                                                new TableOperationCallback<dataAnalysis>() {
                                                    @Override
                                                    public void onCompleted(
                                                            dataAnalysis entity,
                                                            Exception exception,
                                                            ServiceFilterResponse response) {
                                                        if (exception == null) {
                                                        }
                                                    }
                                                });
                            }
                        }
                    });
        }
private void GetLatLon() {
    final LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
            latlon.add(location.getLongitude() + "");
            latlon.add(location.getLatitude() + "");
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };
    LocationManager lm = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0,
            locationListener);
}
private void InitMobileService() {
    try {
        mClient = new MobileServiceClient(
                "",
                "", this);
    } catch (MalformedURLException e) {
        Log.d("ERROR", e.getMessage());
    }
}

private void ClearData() {
    Editor edit = pref.edit();
    edit.clear();
    edit.commit();
}

private void AddData(String key, String value) {
    Editor edit = pref.edit();
    edit.putString(key, value);
    edit.commit();
}

private String GetData(String key) {
    return pref.getString(key, "");
}

Here is my logcat output

02-09 10:34:23.230: E/AndroidRuntime(434): FATAL EXCEPTION: main
02-09 10:34:23.230: E/AndroidRuntime(434): Process: com.example.androidjavaproject, PID: 434
02-09 10:34:23.230: E/AndroidRuntime(434): java.lang.NullPointerException
02-09 10:34:23.230: E/AndroidRuntime(434):  
at com.example.androidjavaproject.AddrampsActivity.dataSave(AddrampsActivity.java:151)
02-09 10:34:23.230: E/AndroidRuntime(434): 
at com.example.androidjavaproject.AddrampsActivity.access$0(AddrampsActivity.java:145)
02-09 10:34:23.230: E/AndroidRuntime(434): 
at com.example.androidjavaproject.AddrampsActivity$1.onClick(AddrampsActivity.java:83)
02-09 10:34:23.230: E/AndroidRuntime(434):  
at android.view.View.performClick(View.java:4445)
02-09 10:34:23.230: E/AndroidRuntime(434):  
at android.view.View$PerformClick.run(View.java:18429)
02-09 10:34:23.230: E/AndroidRuntime(434):  
at android.os.Handler.handleCallback(Handler.java:733)
02-09 10:34:23.230: E/AndroidRuntime(434):  
at android.os.Handler.dispatchMessage(Handler.java:95)
02-09 10:34:23.230: E/AndroidRuntime(434):  at android.os.Looper.loop(Looper.java:136)
02-09 10:34:23.230: E/AndroidRuntime(434):  
at android.app.ActivityThread.main(ActivityThread.java:5087)
02-09 10:34:23.230: E/AndroidRuntime(434):  at
java.lang.reflect.Method.invokeNative(Native Method)
02-09 10:34:23.230: E/AndroidRuntime(434):  at
java.lang.reflect.Method.invoke(Method.java:515)
02-09 10:34:23.230: E/AndroidRuntime(434):  at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-09 10:34:23.230: E/AndroidRuntime(434):  at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-09 10:34:23.230: E/AndroidRuntime(434):  at dalvik.system.NativeStart.main(Native
Method)
CodeNewbie
  • 35
  • 7

1 Answers1

0

Most probably its not getting image drawable.

Replace

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.id.stairsRampsPhoto);

with

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.stairsRampsPhoto);

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • sorry for the missing log cat output. my logcat output clears itself automatically and i haven't fixed that yet. My image is not taken from the drawable folder but rather chosen from a gallery. But thank you for your help anyways =D – CodeNewbie Feb 08 '14 at 16:54