我有“用相机捕获图像”的问题,并将其存储到Firebase中.我认为该代码是正确的,因为它可以与“从图库中选择图像”一起使用.捕获完图像后,该应用程序停止了,并且没有存储在数据库中.我认为这对于android M和N是个问题.我只是看到其他类似的问题,但它们对我不起作用.我为此寻求帮助,因为我不知道解决方案.谢谢. logcat中也存在错误.
@OverrIDeprotected 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(); } }); }}
日志猫:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String androID.net.Uri.getLastPathSegment()’ on a null object reference
解决方法:
对于您的问题,我有一个答案,因为几天前我做了同样的事情.问题在于您的Uri无法获得应有的图像,因此您需要为捕获的图像创建自己的Uri.
您需要转到androID文档:https://developer.android.com/training/camera/photobasics.html#TaskPhotoView
对于您的应用,您还可以将以下代码复制并粘贴到您的主要活动中:
String mCurrentPhotopath;private file createImagefile() throws IOException {// Create an image file nameString 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 intentsmCurrentPhotopath = 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 intentif (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); }}}@OverrIDeprotected 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(); }});}@OverrIDeprotected 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(您只需在“ res”文件夹下创建一个目录,并将其命名为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%-玩得开心!
总结以上是内存溢出为你收集整理的上传图片错误:尝试在空对象引用上调用虚拟方法’java.lang.String android.net.Uri.getLastPathSegment()’全部内容,希望文章能够帮你解决上传图片错误:尝试在空对象引用上调用虚拟方法’java.lang.String android.net.Uri.getLastPathSegment()’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)