2012-11-27 21 views
5

Trong ứng dụng của tôi, tôi nhấn một nút có tên là Pick Photo và nó tải bộ sưu tập. Khi tôi bấm vào một hình ảnh trong bộ sưu tập, các lực lượng ứng dụng đóng và trong logcat của tôi, tôi nhận được như sau:java.lang.RuntimeException: Thất bại trong việc cung cấp kết quả ResultInfo {who = null, request = 1, result = -1, data = intent} đến hoạt động

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/3369 (has extras) }} to activity {cap.shot/cap.shot.LolcatActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2655) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2697) 
    at android.app.ActivityThread.access$2000(ActivityThread.java:124) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:130) 
    at android.app.ActivityThread.main(ActivityThread.java:3806) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
    at cap.shot.LolcatView.loadFromUri(LolcatView.java:137) 
    at cap.shot.LolcatActivity.loadPhoto(LolcatActivity.java:384) 
    at cap.shot.LolcatActivity.onActivityResult(LolcatActivity.java:299) 
    at android.app.Activity.dispatchActivityResult(Activity.java:3988) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2651) 

lolcatactivity.java của tôi là có sẵn ở đây: http://pastebin.com/AVL8CswT lolcatview.java của tôi là có sẵn ở đây: http://pastebin.com/vD7vCBgY

Cảm ơn bạn!

+1

Đây có phải là tất cả các lỗi LogCat? Trớ trêu thay, ứng dụng của bạn bị treo ngay sau nhận xét '// TODO: có an toàn khi cho rằng điều này sẽ luôn là một BitmapDrawable? ', Rõ ràng câu trả lời là:" không ". – Sam

+0

Nó nói "... 11 hơn" nhưng tôi không thể nhìn thấy chúng. Bạn có bất kỳ ý tưởng làm thế nào để sửa chữa nó? – Jack

Trả lời

3

getDrawable sẽ trả về giá trị rỗng trong trường hợp của bạn. Các uri mà bạn đang sử dụng cho setImageURI có thể không hợp lệ, do đó bạn đang nhận được null.

Thực hiện kiểm tra không có giá trị đối với drawable, nếu drawable là null, bạn cần phải bảo lãnh.

Edit:

if(drawable == null) 
    return; 
+0

Làm cách nào để thực hiện một kiểm tra null cho drawable. Và nếu nó là null, giải thích "bạn cần phải bảo lãnh". – Jack

+0

tôi đã chỉnh sửa, thêm vào trước dòng 139 – nandeesh

+0

Ok Tôi đã thêm nó, nó không freak ra nữa yay! Có một vấn đề mới hơn nhưng dễ hơn. Một số hình ảnh đi qua ngân sách VM của tôi vì vậy tôi nhận được một java.lang.OutofMemoryError. Làm cách nào để tăng ngân sách VM để có thể sử dụng nhiều hình ảnh hơn? – Jack

1

tôi đã kiểm tra mã của bạn vì vậy tôi nghĩ rằng bạn cần phải thay thế button action của bạn như thế này

private static final int SELECT_PHOTO = 100 

        Intent photoPickerIntent = new Intent(
          Intent.ACTION_PICK); 
        photoPickerIntent.setType("image/*"); 
        startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

và trong kết quả hoạt động Start của bạn cung cấp cho

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent imageReturnedIntent) { 
    System.out.println("requestcode" + requestCode + "result code " 
      + requestCode + "intentt" + imageReturnedIntent); 

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch (requestCode) { 
    case SELECT_PHOTO: 
     if (resultCode == RESULT_OK) { 

      // InputStream imageStream; 
      try { 
       Uri selectedImage = imageReturnedIntent.getData(); 
       Bitmap yourSelectedImage = decodeUri(selectedImage); 
       // imageStream = getContentResolver().openInputStream(
       // selectedImage); 
       // Bitmap yourSelectedImage = BitmapFactory 
       // .decodeStream(imageStream); 
       try { 
        yourimageview.setImageBitmap(yourSelectedImage); 
        picArray = convertBitmap(yourSelectedImage); 
        String imagepath_new = getRealPathFromURI(selectedImage); 

        System.out.println("gakk" + imagepath_new); 
        String[] s = imagepath_new.split("/"); 
        System.out.println(s[s.length - 1]); 
        String imageName1 = s[s.length - 1]; 
        imageName1 = imageName1.replace(" ", ""); 

       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), 
          "Exception" + e, 1000).show(); 
       } 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     break; 

Và cũng Thêm những dòng này để tránh rò rỉ bộ nhớ trong khi hiển thị chế độ xem hình ảnh trong Hoạt động của bạn.

private byte[] convertBitmap(Bitmap bm) { 
    // int bytes = bm.getWidth() * bm.getHeight() * 4; // calculate how many 
    // bytes our image 
    // consists of. Use a 
    // different value than 
    // 4 if you don't use 
    // 32bit images. 

    // ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new 
    // buffer 
    // bm.copyPixelsToBuffer(buffer); // Move the byte data to the buffer 

    // byte[] array = buffer.array(); // Get the underlying array containing 
    // the data. 

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); 

    byte[] array = stream.toByteArray(); 
    return array; 
} 

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(
      getContentResolver().openInputStream(selectedImage), null, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 140; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
      break; 
     } 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 3; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    return BitmapFactory.decodeStream(
      getContentResolver().openInputStream(selectedImage), null, o2); 

} 

public String getRealPathFromURI(Uri contentUri) { 

    // can post image 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(contentUri, proj, // Which columns to 
                // return 
      null, // WHERE clause; which rows to return (all rows) 
      null, // WHERE clause selection arguments (none) 
      null); // Order-by clause (ascending by name) 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    return cursor.getString(column_index); 

} 

private Bitmap decodeFile(File f) { 
    Bitmap b = null; 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     FileInputStream fis = new FileInputStream(f); 
     BitmapFactory.decodeStream(fis, null, o); 

     fis.close(); 

     int scale = 10; 
     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     fis = new FileInputStream(f); 
     b = BitmapFactory.decodeStream(fis, null, o2); 
     fis.close(); 
    } catch (IOException e) { 

    } 
    return b; 
} 

niềm hy vọng này willl giúp bạn

+0

@ nandeesh đã cho tôi một giải pháp rồi. Mặc dù tôi muốn thử của bạn bởi vì hy vọng nó sẽ vá rò rỉ bộ nhớ. – Jack

+0

Tôi nên thêm bộ chuyển đổi bitmap ở đâu? – Jack

+0

xin lỗi vì phát lại trễ bạn thấy decodeFile (File f) làm giảm kích thước hình ảnh – Ramz

Các vấn đề liên quan