尝试上传照片时出现NullPointerException

时间:2017-03-02 21:37:23

标签: java android camera server

我正在尝试运行一个项目,从相机和图库中获取图像,然后使用多个库将其上传到服务器...图像在被选中后不会显示在图像视图中..因为我不能上传它!  我真的需要帮助

camera.java活动

 public class camera extends AppCompatActivity  {

ImageView ivCamera, ivGallery, ivUpload, ivImage;
CameraPhoto cameraPhoto;
GalleryPhoto galleryPhoto;

final int CAMERA_REQUEST = 1;
final int GALLERY_REQUEST = 2;

private final String TAG = this.getClass().getName();

String selectedPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    cameraPhoto = new CameraPhoto(getApplicationContext());
    galleryPhoto = new GalleryPhoto(getApplicationContext());


    ivImage = (ImageView)findViewById(R.id.ivImage);
    ivCamera = (ImageView)findViewById(R.id.ivCamera);
    ivGallery = (ImageView)findViewById(R.id.ivGallery);
    ivUpload = (ImageView)findViewById(R.id.ivUpload);

    ivCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
                cameraPhoto.addToGallery();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),
                        "Something wrong while taking photo", Toast.LENGTH_SHORT).show();
            }
        }
    });

    ivGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           startActivityForResult(galleryPhoto.openGalleryIntent(),GALLERY_REQUEST);
        }
    });
    ivUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            try {
                Bitmap bitmap = ImageLoader.init().from(selectedPhoto).requestSize(1024,1024).getBitmap();
                String encodedImage = ImageBase64.encode(bitmap);
                Log.d(TAG,encodedImage);

            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),
                        "Something wrong while encoding photo", Toast.LENGTH_SHORT).show();

            }
        }
    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK) {
        if (resultCode == CAMERA_REQUEST) {

            String photoPath = cameraPhoto.getPhotoPath();
            selectedPhoto = photoPath;
            Bitmap bitmap = null;
            try {
                bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
                ivImage.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),
                        "Something wrong while loading photo", Toast.LENGTH_SHORT).show();
            }

        }
    }
        else if (requestCode == GALLERY_REQUEST){

            Uri uri= data.getData();
            galleryPhoto.setPhotoUri(uri);
            String photoPath = galleryPhoto.getPath();
            selectedPhoto = photoPath;
            Bitmap bitmap = null;
            try {
                 bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
                ivImage.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),
                        "Something wrong while choosing photo", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Logcat错误

FATAL EXCEPTION: main
              java.lang.NullPointerException
                  at java.io.File.fixSlashes(File.java:185)
                  at java.io.File.<init>(File.java:134)
                  at com.kosalgeek.android.photoutil.ImageLoader.getBitmap(ImageLoader.java:47)
                  at alsaad.layla.test.camera$3.onClick(camera.java:82)
                  at android.view.View.performClick(View.java:4204)
                  at android.view.View$PerformClick.run(View.java:17355)
                  at android.os.Handler.handleCallback(Handler.java:725)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:5041)
                  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:793)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

请使用以下方法解决您的问题: 使用getInstance()代替init()

String selectedPhoto = "";
try {
            Bitmap bitmap = ImageLoader. getInstance().from(selectedPhoto).requestSize(1024,1024).getBitmap();
            String encodedImage = ImageBase64.encode(bitmap);
            Log.d(TAG,encodedImage);

        } catch (FileNotFoundException e) {
            Toast.makeText(getApplicationContext(),
                    "Something wrong while encoding photo", Toast.LENGTH_SHORT).show();

        }