2016-11-20 134 views
0

编辑:关于这个问题的讨论进行到另一个问题:Image capture with camera & upload to Firebase (Uri in onActivityResult() is null)请检查出来!我做了一些操作(遵循android文档),但仍然遇到同样的问题。上传图片(ACTION_IMAGE_CAPTURE)到Firebase存储

所以我在这里试图做一个简单的任务,用相机捕获图像并将其上传到Firebase上的存储数据库。我有以下按钮来完成这项工作:

b_capture.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      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){ 
     progressDialog.setMessage("Uploading..."); 
     progressDialog.show(); 
     Uri uri = data.getData(); 
     StorageReference filepath = storage.child("Photos").child(uri.getLastPathSegment()); 
     filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
       Toast.makeText(MainActivity.this, "Upload Successful!", Toast.LENGTH_SHORT).show(); 
       progressDialog.dismiss(); 
      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception e) { 
       Toast.makeText(MainActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     } 
    } 

在应用程序中我能拍照,但我确认照片后拍摄应用程序崩溃,并且我收到以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference 

所以,我体会到了“开放的”不拥有任何东西,但我很困惑什么可以做。需要帮忙。

是的,我已经检查的所有权限,火力地堡连接,摇篮同步等

+0

看到这个答案:http://stackoverflow.com/a/40567453/4815718 –

+0

@qbix对不起,错误的问题(我以前的回答)。我已经看到了这个问题,就像它所说的那样 - 仍然行不通。下面的路径讨论现在进入另一个问题:http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul –

+0

@qbix请检查问题:http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul我已经这样做了,它仍然会不为我工作。请帮忙! –

回答

1

您需要重新阅读the documentation,并按照创建一个文件,并存储例如其Uri在摄像意图。这是从文档中,你需要添加代码:

String mCurrentPhotoPath; 

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


b_capture.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      dispatchTakePictureIntent() 

      //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      //startActivityForResult(intent, CAMERA_REQUEST_CODE); 
     } 
    }); 
+0

我已经在评论中回答了你,我已经完成了你所写的内容。如果你想帮助,请检查http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul - 在这里我已经从文档中完成这一部分。编辑:所以是的,我们发现了我与你联系的讨论中的错误;现在代码有效。既然你的答案是正确的(尽管它没有帮助解决问题),我为你付出了努力。谢谢! –

相关问题