我正在为拼图游戏编写一个图像分割方法。除了默认图像,应用程序可以从图库中挑选和分割图像。从库中选择图像并关闭应用程序后,我收到此错误。这是我的代码:
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]);
}
}
错误在线
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
任何人都可以帮助解决上述代码吗?已经尝试添加if(resultCode != RESULT_CANCELED)
或if (data != null)
等条件,但仍然没有结果。任何帮助表示赞赏。提前谢谢。
这里是我的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)
我将代码更改为:
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();
但问题仍然存在。
答案 0 :(得分:-1)
这里有一些问题,其中最重要的是你在没有提供完整堆栈跟踪的情况下问了一个关于崩溃的问题。
导致崩溃的原因可能是您尝试导出picturePath
的无效代码。 A Uri
is not a file。将Uri
视为与Web服务器的URL相同的方式。
除此之外,您正在主应用程序线程上进行I / O(读取图像)和对该图像进行重要处理(解码),这会在完成工作时冻结UI。
有many image loading libraries for Android,例如Universal Image Loader和Picasso。大多数(如果不是全部)可以使用Uri
并将位图异步加载到ImageView
。