2015-10-15 55 views
0

Iam試圖從攝像頭獲取圖像後加載圖像,但iam 「發送結果resultinfo失敗」錯誤。爲什麼意向數據即將爲空Android加載攝像頭圖像時發生結果resultinfo失敗

發送resultCode = -1

requestCode = 0

數據= NULL

:下面是值當我得到這個錯誤嗎?這段代碼有什麼問題?

int REQUEST_CAMERA = 0 

private void selectImage() { 
    final CharSequence[] items = { "Take Photo", "Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileSettings.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { 

     if (items[item].equals("Take Photo")) 
     { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"))); 

      startActivityForResult(intent, REQUEST_CAMERA); 
     } 
     else if (items[item].equals("Cancel")) 
     { 
      dialog.dismiss(); 
     } 
    } 
    }); 

    builder.show(); 
} 


@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_CAMERA) 
      onCaptureImageResult(data); 
    } 
} 

private void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".jpg"); 

    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    mainImage.setImageBitmap(thumbnail); 
} 

回答

0

why the Intent data is coming as null?

您提供EXTRA_OUTPUT。如果您這樣做,則無需處理您的請求的相機應用程序通過Intent返回結果。畢竟,您不需要結果,因爲您知道全尺寸圖像的寫入位置:您通過EXTRA_OUTPUT指示的位置。

+0

我們如何解決這個問題? – Abhi1988

+0

@ Abhi1988:停止嘗試使用'Intent',並使用您在'EXTRA_OUTPUT'中提供的位置。 – CommonsWare

相關問題