2013-05-09 95 views
0

我試圖將我的相機應用程序連接到窗口天藍色的blob存儲。這是我的應用程序的工作原理。它需要照片然後顯示在相機類的imageview上。將會有一個報告按鈕,用戶可以從圖庫中選擇圖片,然後通過SASURL發送。不幸的是,我在報告類文件中收到一個錯誤,我無法找到錯誤的位置。azure blob上的nullpointerexception

下面是我的相機類代碼:

public class Camera extends Activity{ 

ImageView iv; 
InputStream is; 
Button btnR, btnP, btnC; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    btnP = (Button)findViewById(R.id.btnPhoto); 
    btnR = (Button)findViewById(R.id.btnReport); 
    btnC = (Button)findViewById(R.id.btnCancel); 
    iv = (ImageView) findViewById(R.id.imageView1); 

    btnP.setOnClickListener(new OnClickListener(){ 

     public void onClick(View v){ 
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, 0); 
     } 
    }); 

     btnC.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       Intent intent = new Intent(Camera.this, MainMenu.class); 
       startActivity(intent); 
      } 
     }); 

     btnR.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       Intent intent = new Intent(Camera.this, Report.class); 
       startActivity(intent); 
      } 
     }); 

} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if(requestCode == 0) 
    { 
     Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 
    } 
} 

} 

下面是我的報告類代碼:

public class Report extends Activity { 
private StorageService mStorageService; 
private final String TAG = "BlobsActivity"; 
private String mContainerName; 
private ImageView mImgBlobImage; 
private Uri mImageUri; 
private AlertDialog mAlertDialog; 

Button btnSelect, btnR; 
ImageView iv; 
TextView tv1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Get access to the storage service 
    StorageApplication myApp = (StorageApplication) getApplication(); 
    mStorageService = myApp.getStorageService(); 
    //Get data from the intent that launched this activity 
    Intent launchIntent = getIntent(); 
    mContainerName = launchIntent.getStringExtra("ContainerName"); 

    //Get the blobs for the selected container 
    mStorageService.getBlobsForContainer(mContainerName);  


     setContentView(R.layout.report); 
     tv1=(TextView) findViewById(R.id.editText1);    
     btnR = (Button)findViewById(R.id.btnReport); 
     iv = (ImageView) findViewById(R.id.imageView1); 
     btnSelect = (Button)findViewById(R.id.btnSelect); 
     //Set select image handler 
     btnSelect.setOnClickListener(new OnClickListener() {     
      @Override 
      public void onClick(View v) { 
       selectImage(); 
      } 
     }); 

btnR.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    mStorageService.getSasForNewBlob(mContainerName, tv1.getText().toString()); 
    } 
}); 

    { 

    JsonObject blob = mStorageService.getLoadedBlob(); 
    String sasUrl = blob.getAsJsonPrimitive("sasUrl").toString(); 
    (new ImageUploaderTask(sasUrl)).execute(); 
    } 
    } 

// Fire off intent to select image from gallery 
protected void selectImage() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    startActivityForResult(intent, 1111); 
} 

// Result handler for any intents started with startActivityForResult 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     //handle result from gallary select 
     if (requestCode == 1111) { 
      Uri currImageURI = data.getData(); 
      mImageUri = currImageURI; 
      //Set the image view's image by using imageUri 
      mImgBlobImage.setImageURI(currImageURI); 
     } 
    } catch (Exception ex) { 
     Log.e(TAG, ex.getMessage()); 
    } 
} 

/*** 
* Handles uploading an image to a specified url 
*/ 
class ImageUploaderTask extends AsyncTask<Void, Void, Boolean> { 
    private String mUrl; 
    public ImageUploaderTask(String url) { 
     mUrl = url; 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) {   
     try { 
      //Get the image data 
      Cursor cursor = getContentResolver().query(mImageUri, null,null, null, null); 
      cursor.moveToFirst(); 
      int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
      String absoluteFilePath = cursor.getString(index); 
      FileInputStream fis = new FileInputStream(absoluteFilePath); 
      int bytesRead = 0; 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      byte[] b = new byte[1024]; 
      while ((bytesRead = fis.read(b)) != -1) { 
       bos.write(b, 0, bytesRead); 
      } 
      byte[] bytes = bos.toByteArray(); 
      // Post our image data (byte array) to the server 
      URL url = new URL(mUrl.replace("\"", "")); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setDoOutput(true); 
      urlConnection.setRequestMethod("PUT"); 
      urlConnection.addRequestProperty("Content-Type", "image/jpeg"); 
      urlConnection.setRequestProperty("Content-Length", ""+ bytes.length); 
      // Write image data to server 
      DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); 
      wr.write(bytes); 
      wr.flush(); 
      wr.close(); 
      int response = urlConnection.getResponseCode(); 
      //If we successfully uploaded, return true 
      if (response == 201 
        && urlConnection.getResponseMessage().equals("Created")) { 
       return true; 
      } 
     } catch (Exception ex) { 
      Log.e(TAG, ex.getMessage()); 
     } 
     return false;   
    } 

    @Override 
    protected void onPostExecute(Boolean uploaded) { 
     if (uploaded) { 
      mAlertDialog.cancel(); 
      mStorageService.getBlobsForContainer(mContainerName); 
     } 
    } 
} 
} 

logcat的錯誤:

05-09 05:56:32.585: E/AndroidRuntime(1869): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.example.testproject/com.example.testproject.Report}: java.lang.NullPointerException 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.os.Looper.loop(Looper.java:137) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at dalvik.system.NativeStart.main(Native Method) 
05-09 05:56:32.585: E/AndroidRuntime(1869): Caused by: java.lang.NullPointerException 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at com.example.testproject.Report.onCreate(Report.java:77) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.Activity.performCreate(Activity.java:5008) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-09 05:56:32.585: E/AndroidRuntime(1869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
+0

Report.java 77的行是什麼? – 2013-05-09 06:10:37

+0

您從哪個活動開始報告? – 2013-05-09 06:18:15

+0

report.java class line 77你的意思是? 這 '字符串sasUrl = blob.getAsJsonPrimitive( 「sasUrl」)的toString();' – 2013-05-09 06:21:22

回答

0

在該行的blob對象String sasUrl = blob.getAsJsonPrimitive("sasUrl").toString();是空值。所以它拋出了NullPointerException,因爲你試圖調用一個對象的方法是null

確保mStorageService.getLoadedBlob()不返回null

+0

我怎樣才能不通過null blob ?我不太確定我是否錯過了任何重要的代碼。 – 2013-05-09 09:20:27

+0

對不起,我不知道天青。 – 2013-05-09 09:23:56