I'm trying to upload an image to my album VC. I use this code , for a long time I can not figure it out, so I try different ways. In each of them I manage to pass authorization, and then: in the first method, nothing happens, in the second, the exception specified below is dropped and the application is closed, in the third, similarly to the second. How to fix the error?

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10485, result=-1, data=null} to activity {com.example.molodec.vktest/com.example.molodec.vktest.MainActivity}: 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 File photo = new File("android.jpg"); Первый способ final Bitmap photo = getPhoto(); Второй способ BitmapFactory.decodeStream(getAssets().open("android.jpg")); Третий способ 
  • You may not have permission to read files in the manifest. Perhaps it is, but you are testing on Android 6 and there you are not requesting permission during execution. - Yuriy SPb

1 answer 1

The method loads the picture for posting on the wall. After its execution, you must perform wall.post with the necessary parameters. Study this code carefully:

 final Bitmap photo = getPhoto(); VKRequest request = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, TARGET_GROUP); request.executeWithListener(new VKRequestListener() { @Override public void onComplete(VKResponse response) { recycleBitmap(photo); VKApiPhoto photoModel = ((VKPhotoArray) response.parsedModel).get(0); makePost(new VKAttachments(photoModel)); } @Override public void onError(VKError error) { showError(error); } }); private void makePost(VKAttachments attachments) { makePost(attachments, null); } private void makePost(VKAttachments attachments, String message) { VKRequest post = VKApi.wall().post(VKParameters.from(VKApiConst.OWNER_ID, "-" + TARGET_GROUP, VKApiConst.ATTACHMENTS, attachments, VKApiConst.MESSAGE, message)); post.setModelClass(VKWallPostResult.class); post.executeWithListener(new VKRequestListener() { @Override public void onComplete(VKResponse response) { if (isAdded()) { VKWallPostResult result = (VKWallPostResult) response.parsedModel; Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("https://vk.com/wall-%d_%s", TARGET_GROUP, result.post_id))); startActivity(i); } } @Override public void onError(VKError error) { showError(error.apiError != null ? error.apiError : error); } }); 
  • I copied getPhoto () from the source of the test application vkandroidsk and failed to import the Activity dependency in the method. Also, I didn’t understand where to get the dependency for recyleBitmap, but I was not found at all - Daineal28
  • @bolshoymolodec If you have a problem with transcoding, then try this code snippet: Bitmap myBitmap = BitmapFactory.decodeResource (getResources (), R.drawable.post); In bitmap there will be an image recoded from post.png, which you have in drawable - Vladimir
  • @bolshoymolodec and also read here info : new.vk.com/dev/upload_files - Vladimir