2015-05-22 86 views
0

我寫拼圖遊戲的圖像分割方法。除了默認圖片,應用程序還可以從圖庫中挑選並分割圖片。從圖庫中選擇圖片並關閉應用程序後,出現此錯誤。這裏是我的代碼:未能提供結果ResultInfo {誰= NULL,請求= 1,結果= -1,數據=意向{DAT =內容://媒體/外部/圖像/媒體/ 57054(有演員)}}

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(); 

但問題依然存在。

回答

-1

你有幾個問題在這裏,而不是其中最重要的是,你問了一個崩潰有關的問題,而不提供一個完整的堆棧跟蹤。

什麼是可能導致您的崩潰是你的無效代碼試圖獲得picturePathA Uri is not a file。對待Uri的方式與您將Web服務器的URL相同。

除此之外,你正在做的I/O(讀取圖像)和圖像的顯著處理主應用程序線程,而這項工作正在做它凍結您的UI上(解碼的話)。

many image loading libraries for Android,諸如通用圖像裝載機和畢加索。大多數情況下,他們如果不是全部,可以把你的Uri和位圖異步地加載到ImageView

+0

那麼你的建議是什麼? – user3417256

+0

@ user3417256:使用其中一個圖像加載庫,以異步方式將「Uri」的內容加載到「ImageView」中。您還沒有初始化'image' AFAICT,它可能是'NullPointerException'的來源。 – CommonsWare

相關問題