I'm writing an Image splitting method for a jigsaw puzzle game. Beside the default image the app can pick and split an image from the gallery. I get this error after choosing an image from the gallery and the app force close. Here are my code:
ImageView image;
Uri selectedImage;
private final int RESULT_LOAD_IMAGE = 1;
int chunkNumbers = 16;
ArrayList<Bitmap> chunkedImages;
Button[] buttons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
choose = (Button) findViewById(R.id.ImgPicker);
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickImageFromGallery();
}
});
}
void pickImageFromGallery() {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=Activity.RESULT_CANCELED) {
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == Activity.RESULT_OK) {
// takenPictureData = handleResultFromChooser(data);
selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
splitImage(image, chunkNumbers);
/*if(selectedImage!=null) {
try {
InputStream picturePath = getContentResolver().openInputStream(selectedImage);
Bitmap bm = BitmapFactory.decodeStream(picturePath);
// Function of split the image(divide the image into pieces)
splitImage(image, chunkNumbers);
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
}
// ImageView imageView = (ImageView) findViewById(R.id.imgView);
break;
}
}
}
private void splitImage(ImageView image, int chunkNumbers) {
//For the number of rows and columns of the grid to be displayed
int rows,cols;
//For height and width of the small image chunks
int chunkHeight,chunkWidth;
//To store all the small image chunks in bitmap format in this list
chunkedImages = new ArrayList<Bitmap>(chunkNumbers);
//Getting the scaled bitmap of the source image
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rows = cols = (int) Math.sqrt(chunkNumbers);
chunkHeight = bitmap.getHeight()/rows;
chunkWidth = bitmap.getWidth()/cols;
//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
BitmapDrawable[] bmd = (BitmapDrawable[]) chunkedImages.toArray();
for(int i=0;i<15;i++)
{
buttons[i].setBackgroundDrawable(bmd[i]);
}
}
The error is on line
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Can anyone help with the above code? Already tried adding condition like if(resultCode != RESULT_CANCELED)
or if (data != null)
but still no result. Any help is appreciated. Thanks in advance.
Here my logcat:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/57054 (has extras) }} to activity {com.example.duc.puzzledemo/com.example.duc.puzzledemo.Game}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3433)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476)
at android.app.ActivityThread.access$1200(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.duc.puzzledemo.Game.onActivityResult(Game.java:350)
at android.app.Activity.dispatchActivityResult(Activity.java:5515)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3429)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476)
at android.app.ActivityThread.access$1200(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
I changed my code to :
try {
InputStream picturePath = getContentResolver().openInputStream(selectedImage);
Bitmap bm = BitmapFactory.decodeStream(picturePath);
image.setImageBitmap(bm);
// Function of split the image(divide the image into pieces)
splitImage(image, chunkNumbers);
} catch (FileNotFoundException e) {
e.printStackTrace();
But the problem still exist.