2017-02-13 150 views
2

我非常抱歉,如果這可能是一個簡單的解決方案的問題。捕獲圖像,上傳到Firebase並檢索Java Android Studio

我該怎麼做?

  • 捕獲來自按鈕
  • 圖像上載圖像火力地堡存儲
  • 檢索圖像中的ImageView的

什麼是我的麻煩這麼遠?

  • 拍照,但當我點擊打勾時崩潰。
  • 因此沒有上傳或正在實現。

我的代碼

P.N我已經看過很多其他論壇和視頻教程,但沒有的似乎是工作。希望有人能幫助。

public class LeaderboardActivity extends AppCompatActivity { 

    private static final int CAMERA_REQUEST_CODE = 1; 
    private static final int REQUEST_TAKE_PHOTO = 1; 
    private StorageReference mStorage; 

    private ProgressDialog mProgress; 

    String mCurrentPhotoPath; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_leaderboard); 

     mStorage = FirebaseStorage.getInstance().getReference(); 

     final Button bImage = (Button) findViewById(R.id.bCamera); 
     final ImageView ivPic = (ImageView) findViewById(R.id.ivPic); 

     mProgress = new ProgressDialog(this); 

     bImage.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, CAMERA_REQUEST_CODE); 


      } 
     }); 
    } 

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

     if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) { 

      mProgress.setMessage("Uploading Image"); 
      mProgress.show(); 

      Uri uri = data.getData(); 
      StorageReference filepath = mStorage.child("Photo").child(uri.getLastPathSegment()); 
      filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener <UploadTask.TaskSnapshot>() { 
       @Override 
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
        mProgress.dismiss(); 
        Toast.makeText(LeaderboardActivity.this, "Uploading Complete...", Toast.LENGTH_LONG); 

       } 
      }); 
     } 
    } 

    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg", /* suffix */ 
      storageDir /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 

    private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // Ensure that there's a camera activity to handle the intent 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 
       // Error occurred while creating the File 

      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       Uri photoURI = FileProvider.getUriForFile(this, 
        "com.example.android.fileprovider", 
        photoFile); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
      } 
     } 
    } 
} 

錯誤在Android的監視器

不知道這將有助於

02-13 02:30:32.693 2133-2133/com.example.rikhi.chores E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.rikhi.chores, PID: 2133 
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.rikhi.chores/com.example.rikhi.chores.LoginRegister.InsideMainActivity.LeaderboardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4089) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference 
    at com.example.rikhi.chores.LoginRegister.InsideMainActivity.LeaderboardActivity.onActivityResult(LeaderboardActivity.java:74) 
    at android.app.Activity.dispatchActivityResult(Activity.java:6932) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4085) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)  
    at android.app.ActivityThread.-wrap20(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:154)  
    at android.app.ActivityThread.main(ActivityThread.java:6119)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

我已經看過其他人誰有同樣的問題,然後他們說什麼happend,其次谷歌Android的說明,這又是同樣的問題。

+0

我不使用firebase,但使用AsyncHttpClient將HTTP上傳請求上傳到服務器非常簡單 –

+0

不幸的是,我剛剛開始使用Android Studio,並且已經創建了登錄數據庫而試圖讓這個工作起作用的用戶是XD的最後一步,但是謝謝你不好意思看一下 –

回答

1
  1. 上的開關按鈕,單擊刪除意圖,只是叫你 dispatchTakePictureIntetnt()上的onclick按鈕監聽方法

  2. 此外,檢查到你的模擬器的設置,並檢查您的相機 權限是對你的這個應用程序,並進入firebase控制檯 並檢查規則,如果規則不等於空使它們等於 爲空然後運行您的應用程序,因爲!=null規則僅在 是您的應用程序中的驗證方法時才起作用