2016-11-19 13 views
2

「カメラで画像をキャプチャ」に問題があり、Firebaseに保存しています。私はコードが正しいと思う。なぜなら、それは「ギャラリーから画像を選択する」と機能するからだ。アプリはキャプチャー画像の後に停止し、データベースには保存されません。私はそれがアンドロイドMとNのための問題だと思う。私は他の同様の質問を見ているが、私のために働いていない。私は解決策を知らないので、私はこれについて助けを求める。ありがとう。 logcatにもエラーがあります。画像アップロードエラー:ヌルオブジェクトリファレンスで仮想メソッド 'java.lang.String android.net.Uri.getLastPathSegment()'を呼び出そうとしました

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


    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){ 
     mPregresDialog.setMessage("Uploading..."); 
     mPregresDialog.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) { 
       mPregresDialog.dismiss(); 

       Toast.makeText(MainActivity.this, "Upload Done", Toast.LENGTH_LONG).show(); 

      } 
     }); 

    }} 

logcat:

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/q/40567245/4815718この回答を見てください:http://stackoverflow.com/a/40567453/4815718 –

+0

ありがとうあなたは非常に。私はこの同様の質問を失っています。私はドキュメントに従って、私は自分の問題を解決しました。ありがとう! – hulon

答えて

2

私はちょうど数日前に同じことをしたので、私は、あなたの問題の答えを持っています。問題は、Uriがイメージを取得できないため、キャプチャしたイメージ用に独自のUriを作成する必要があることです。

あなたはアンドロイドのドキュメントに移動する必要があります:アプリのhttps://developer.android.com/training/camera/photobasics.html#TaskPhotoView

、あなたはまた、単にコピーしてあなたの主な活動で、このコードを貼り付けることができます。

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, CAMERA_REQUEST_CODE); 
    } 
} 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

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

b_gallery = (Button) findViewById(R.id.b_gallery); 
b_capture = (Button) findViewById(R.id.b_capture); 
iv_image = (ImageView) findViewById(R.id.iv_image); 

progressDialog = new ProgressDialog(this); 

b_capture.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     dispatchTakePictureIntent(); 

    } 
}); 
} 

@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(photoURI).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(); 
     } 
    }); 
    } 
} 
} 

そして、あなたはドキュメントに従っていることを確認し、あなたのAndroidManifest.xmlでプロバイダを追加しました:

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.example.android.fileprovider" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths"></meta-data> 
</provider> 

そしてRES/XML/file_paths.xml(あなただけの「解像度」フォルダと名前でディレクトリを作成しますXMLファイルを作成し、それにfile_paths.xmlという名前を付けます)。

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
<external-path name="my_images"  path="Android/data/com.serjardovic.firebasesandbox/files/Pictures" /> 
</paths> 

独自のパッケージ名にcom.serjardovic.firebasesandboxを変更することを確認してください。その後、すべてそれ内のコードを削除するには、次の(それはいくつかの行があるでしょう)して貼り付け!

100%働いています - 楽しんでください!

+1

ありがとう!それは動作します! – hulon

関連する問題

 関連する問題