照片相机上传例外(Firebase存储) [英] Photo by Camera Upload Exception (Firebase Storage)
问题描述
这是图像,当我按下检查按钮,应用程序崩溃...
$ b
这是我的代码,应用程序可以选择使用图库和相机上传图片。
import android.support.v7.app.AppCompatActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.View;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
导入com.google.firebase.storage.FirebaseStorage;
导入com.google.firebase.storage.StorageReference;
导入com.google.firebase.storage.UploadTask;
公共类MainActivity扩展AppCompatActivity {
//注意:发货前,您的消费者密钥和密码应该在您的源代码中被混淆。
私人按钮selectImage;
私人按钮selectImageByCamera;
私人ImageView imageView;
私有StorageReference storageReference;
私人ProgressDialog progressDialog;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int GALLERY_INTENT = 2;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
storageReference = FirebaseStorage.getInstance()。getReference();
/ *
*代码段使用图库上传图片。
* /
selectImage =(Button)findViewById(R.id.btn_uploadImg);
progressDialog = new ProgressDialog(this);
selectImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
意图intent = new Intent(Intent.ACTION_PICK);
intent.setType(image / *);
startActivityForResult(intent,GALLERY_INTENT);
}
} );
/ *
*代码部分使用相机上载图像。
* /
selectImageByCamera =(Button)findViewById(R.id.btn_uploadImgCamera);
imageView =(ImageView)findViewById(R.id.imageView);
selectImageByCamera.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(MediaStore .ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_REQUEST_CODE);
}
});
$ b $覆盖
保护void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode,resultCode,data);
//将图片上传到Firebase更新
if(requestCode == GALLERY_INTENT&& resultCode == RESULT_OK)
{
progressDialog.setMessage(Uploading图片...);
progressDialog.show();
Uri uri = data.getData();
StorageReference filepath = storageReference.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 done,Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
});
}
//将摄像头拍摄的图片上传到firebase存储的代码
if(requestCode == CAMERA_REQUEST_CODE&& resultCode == RESULT_OK)
{
progressDialog.setMessage(上传图片...);
progressDialog.show();
Uri uri2 = data.getData();
StorageReference filepath = storageReference.child(Photos)。child(uri2.getLastPathSegment());
filepath.putFile(uri2).addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override $ b $ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){
{
Toast.makeText(MainActivity.this,Upload done,Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item){
//处理操作栏项目点击这里。操作栏将
//自动处理Home / Up按钮上的点击,只要您在AndroidManifest.xml中指定了一个父活动,就可以获得长
的值。
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if(id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
$ p $在AndroidManifest.xml中有这个用法PERMISSION: / p>
<使用权限android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.READ_EXTERNAL_STORAGE/>
<使用权限android:name =android.permission.CAMERA/>
我在build.gradle中编译了这个Firebase
compile'com.google.firebase:firebase-core:9.8.0'
compile'com.google.firebase:firebase-database:9.8.0'
compile'com.google.firebase:firebase-storage:9.8.0'
compile'com.google.firebase:firebase-auth:9.8.0'
最后这是我在Moto G4 6.0.1中运行应用程序时引发的异常(应用程序有权使用相机和图库)
致命例外:main
过程:mx.com.jamba.jamba,PID:16543
java.lang。 RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = Intent {act = inline-data(has extras)}}传递给活动{mx.com.jamba.jamba / mx.com。 jamba.jamba.MainActivity}:java.lang.NullPointerException:试图调用虚拟方法'java.lang.String android.net.Uri.getLastPathSegment()'对空引用
at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.access $ 1400(ActivityThread.java:154)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)
at com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
导致:java.lang.NullPointerException:试图调用虚拟方法'java.lang.String android.net.Uri.getLastPathSegment()'在空对象引用
在mx.com.jamba.jamba.MainActivity.onActivityResult(MainActivity.java:186)
在android.app.Activity.dispatchActivityResult(Activity.java:6470)
在android.app.ActivityThread。 deliverResults(ActivityThread.java:3716)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.access $ 1400(ActivityThread.java:154)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop( Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我不知道该怎么做,这将会非常棒,你们帮我:)
谢谢!
解决方案 由于 Uri
code> onActivityResult()为null:
$ b $ pre $ U $ uri = data.getData() ;
Android Camera应用程序保存一个全尺寸的照片,如果你给它
a文件保存到。 您必须提供完全合格的文件名称
,相机应用程序应该保存照片。
按照文档中的例子创建一个文件Uri并将其添加到相机应用程序的意图。
I'm trying to upload a image taken by the camera in an android application to Firebase storage. The problem is after I take the picture, in the confirmation 'activity', I press the confirm button and it says that "Unfortunately the application stopped".
This is the image, when I press the check button, the app crashes...
This is my code, the application has the option to upload pictures using the gallery and the camera.
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
public class MainActivity extends AppCompatActivity {
// Note: Your consumer key and secret should be obfuscated in your source code before shipping.
private Button selectImage;
private Button selectImageByCamera;
private ImageView imageView;
private StorageReference storageReference;
private ProgressDialog progressDialog;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int GALLERY_INTENT= 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
storageReference=FirebaseStorage.getInstance().getReference();
/*
* Code section to upload an image using the Gallery.
*/
selectImage=(Button)findViewById(R.id.btn_uploadImg);
progressDialog = new ProgressDialog(this);
selectImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_INTENT);
}
});
/*
* Code section to upload an image using the Camera.
*/
selectImageByCamera=(Button)findViewById(R.id.btn_uploadImgCamera);
imageView=(ImageView)findViewById(R.id.imageView);
selectImageByCamera.setOnClickListener(new View.OnClickListener()
{
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);
//Upload the image to Firebase Update
if(requestCode==GALLERY_INTENT && resultCode == RESULT_OK)
{
progressDialog.setMessage("Uploading Image...");
progressDialog.show();
Uri uri = data.getData();
StorageReference filepath = storageReference.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 done", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
});
}
//Code to upload image taken by the camera to firebase storage
if(requestCode==CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
{
progressDialog.setMessage("Uploading Image...");
progressDialog.show();
Uri uri2 = data.getData();
StorageReference filepath = storageReference.child("Photos").child(uri2.getLastPathSegment());
filepath.putFile(uri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
{
Toast.makeText(MainActivity.this, "Upload done", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have this USES PERMISSION in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
I have this Firebase compile in build.gradle
compile 'com.google.firebase:firebase-core:9.8.0'
compile 'com.google.firebase:firebase-database:9.8.0'
compile 'com.google.firebase:firebase-storage:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'
And finally this is the exception thrown when I run the app in my Moto G4 6.0.1 (The app has permissions to use Camera and Gallery)
FATAL EXCEPTION: main
Process: mx.com.jamba.jamba, PID: 16543
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {mx.com.jamba.jamba/mx.com.jamba.jamba.MainActivity}: 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:3720)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.access$1400(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
at mx.com.jamba.jamba.MainActivity.onActivityResult(MainActivity.java:186)
at android.app.Activity.dispatchActivityResult(Activity.java:6470)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.access$1400(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I don't know what to do, it will be great you guys to help me :)
Thank you!
解决方案 The exception occurs because the Uri
in onActivityResult()
is null:
Uri uri = data.getData();
The documentation for capturing a camera image explains:
The Android Camera application saves a full-size photo if you give it
a file to save into. You must provide a fully qualified file name
where the camera app should save the photo.
Follow the example in the documentation to create a file Uri and add it to the intent for the camera app.
这篇关于照片相机上传例外(Firebase存储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文