Java android将imageview图像转换为base64空指针异常

时间:2014-02-08 16:41:25

标签: java android azure imageview base64

我想问一下是否有办法将图像视图图像(图像从图库/摄像头捕获设置)转换为base64字符串?我尝试了多种方法,但我一直得到空指针异常。有没有人有解决这个问题的想法?我将我的base64字符串存储到windows azure中。我试过把这个方法放入。请注意我删除了我的api密钥和initmobileservice方法的网址,所以请理解并提前感谢

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, "");
}

这是我的logcat输出

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)

1 个答案:

答案 0 :(得分:0)

很可能它没有得到图像可绘制。

替换

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

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

希望它有所帮助。