java对象引用出错_上传图片错误:尝试在空对象引用上调用虚拟方法’java.lang.String android.net.Uri.getLastPathSegment()’...
我有“用相機(jī)捕獲圖像”的問題,并將其存儲(chǔ)到Firebase中.我認(rèn)為該代碼是正確的,因?yàn)樗梢耘c“從圖庫中選擇圖像”一起使用.捕獲完圖像后,該應(yīng)用程序停止了,并且沒有存儲(chǔ)在數(shù)據(jù)庫中.我認(rèn)為這對(duì)于android M和N是個(gè)問題.我只是看到其他類似的問題,但它們對(duì)我不起作用.我為此尋求幫助,因?yàn)槲也恢澜鉀Q方案.謝謝. logcat中也存在錯(cuò)誤.
@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() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mPregresDialog.dismiss();
Toast.makeText(MainActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
}
});
}}
日志貓:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.net.Uri.getLastPathSegment()’ on a null object reference
解決方法:
對(duì)于您的問題,我有一個(gè)答案,因?yàn)閹滋烨拔易隽送瑯拥氖虑?問題在于您的Uri無法獲得應(yīng)有的圖像,因此您需要為捕獲的圖像創(chuàng)建自己的Uri.
對(duì)于您的應(yīng)用,您還可以將以下代碼復(fù)制并粘貼到您的主要活動(dòng)中:
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() {
@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中添加了提供程序:
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
還有res / xml / file_paths.xml(您只需在“ res”文件夾下創(chuàng)建一個(gè)目錄,并將其命名為xml,然后創(chuàng)建一個(gè)資源文件,并將其命名為file_paths.xml);然后刪除其中的所有代碼(會(huì)有幾行)并粘貼以下內(nèi)容:
確保將com.serjardovic.firebasesandbox更改為您自己的包名稱!
效果100%-玩得開心!
標(biāo)簽:firebase,camera,firebase-storage,android,image-uploading
來源: https://codeday.me/bug/20191026/1937019.html
總結(jié)
以上是生活随笔為你收集整理的java对象引用出错_上传图片错误:尝试在空对象引用上调用虚拟方法’java.lang.String android.net.Uri.getLastPathSegment()’...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES6-17 class与对象
- 下一篇: ES6-18/19 异步的开端-prom