0

I'm creating image upload from pick gallery to server. one week ago everything work fine, but today didn work and giving this error.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/61489 flg=0x1 (has extras) }} to activity {com.example.pmb/com.example.pmb.Uploadlagi}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4619)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4661)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
    at com.example.pmb.Uploadlagi.BitMapToString(Uploadlagi.java:140)
    at com.example.pmb.Uploadlagi.onActivityResult(Uploadlagi.java:134)

And here the script

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 2) {
            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));
            IDProf.setImageBitmap(thumbnail);
            BitMapToString(thumbnail);   <------- error mention to this line
        }
    }
}
public String BitMapToString(Bitmap userImage1) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    userImage1.compress(Bitmap.CompressFormat.JPEG, 80, baos);
    byte[] b = baos.toByteArray();
    Document_img1 = Base64.encodeToString(b, Base64.DEFAULT);
    return Document_img1;
}

what the problem? and what must be fixed?

ps: I'm testing on samsung A6

1 Answer 1

0

I found the solution, when i scroll up the log, i found that permission not granted, so in onCreate() function, i create permission request.

if (ContextCompat.checkSelfPermission(Uploadlagi.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(Uploadlagi.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

and create an override function,

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode==1){
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),"Ijin diperbolehkan, silahkan pilih gambar",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(),"Ijin gagal, Anda tidak bisa upload dokumen",Toast.LENGTH_LONG).show();
        }
        return;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.