有关将图像转换为Base64图像的问题

时间:2016-09-20 06:20:17

标签: android base64

This is Image feathing method

 private void selectImage() {

        final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};

        AlertDialog.Builder builder = new AlertDialog.Builder(AddServiceActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo")) {

                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 100);

                } else if (options[item].equals("Choose from Gallery")) {

                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 10);


                } else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

This is OnActivityResult code

public void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (resultCode == RESULT_OK) {
            if (data.getData() != null) {
                selectedImageUri = data.getData();
            } else {
                Log.d("selectedPath1 : ", "Came here its null !");
                Toast.makeText(getApplicationContext(), "failed to get Image!", Toast.LENGTH_SHORT).show();
            }

            if (requestCode == 100 && resultCode == RESULT_OK) {

                Bitmap photo = (Bitmap) data.getExtras().get("data");
                selectedPath = getPath(selectedImageUri);
                camera.setImageURI(selectedImageUri);
                Log.d("selectedPath1 : ", selectedPath);
                getimage(selectedImageUri);

            }

            if (requestCode == 10)

            {


                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from ", picturePath + "");
                camera.setImageBitmap(thumbnail);

                getimage(selectedImage);


            }

        }
    }

Image changing code id here

private void getimage(final Uri uri) {

        Bitmap image = null;
        WeakReference<Bitmap> weakReference = new WeakReference<Bitmap>(new BitmapDrawable(getResources(), uri.getPath()).getBitmap());
        int nh = (int) (weakReference.get().getHeight() * (512.0 / weakReference.get().getWidth()));
        image = Bitmap.createScaledBitmap(weakReference.get(), 512, nh, true);
        Log.e("", "bitmap size is2:" + "" + nh + ":" + image.getByteCount());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        Log.e("", "encoded::" + encoded);
        Log.d("", "encoded::" + encoded);
//        user_imagebase64 = encoded;
        try {
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

代码在getimage方法中一直崩溃,当我调用此方法时,应用程序崩溃了 &#34; int nh =(int)(weakReference.get()。getHeight()*(512.0 / weakReference.get()。getWidth()));&#34;

并且出现错误:java.lang.NullPointerException:尝试调用虚方法&#39; int android.graphics.Bitmap.getHeight()&#39;在空对象引用上

我不知道问题是什么,我也不能使用这个代码..

0 个答案:

没有答案