-1

I am trying to run a project that get image from camera and gallery and then uploads it to the server using more than one library..the image is not shown in image view after it being selected.. for that i can not upload it! I really need help

camera.java activity

 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 error

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)
layla7
  • 53
  • 6
  • My guess is that `selectedPhoto` is `null`. Also note that `getPath()` on a `Uri` is unlikely to work, as that only has meaning if the scheme of the `Uri` is `file`, and you may get other schemes (e.g., `content`). – CommonsWare Mar 02 '17 at 21:39
  • Possible duplicate of [android java.io.File.fixSlashes(File.java:185)](http://stackoverflow.com/questions/31172711/android-java-io-file-fixslashesfile-java185) – nikis Mar 02 '17 at 21:41
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jason Mar 02 '17 at 21:44
  • @CommonsWare so what should I do? – layla7 Mar 02 '17 at 21:44
  • 1
    Add logging statements or use a debugger to see what the value is at runtime. – Jason Mar 02 '17 at 21:48
  • You should be using the `Uri` directly with your image-loading library, rather than attempting to get some string for it. Any decent image-loading library knows how to load images from a `Uri`, for both `file` and `content` schemes. – CommonsWare Mar 02 '17 at 21:49

1 Answers1

0

Please use below method to fix your issue : Use getInstance() instead of 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();

        }
Narendra Sorathiya
  • 3,426
  • 2
  • 30
  • 35