我要做的是捕获图像并在图像视图中查看它并将其存储到firebase存储中。
我的代码:
public class WaterConsumptionPage extends Fragment {
private ImageView imageView;
private Button capture, upload;
static final int CAM_REQUEST = 1;
private StorageReference mStorageRef;
private FirebaseAuth mAuth;
private DatabaseReference dbRef;
private ProgressDialog mProgress;
private Bitmap photo;
private FirebaseUser user;
private Uri uri;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.water_consumption_page, container, false);
mStorageRef = FirebaseStorage.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
dbRef = FirebaseDatabase.getInstance().getReference();
user = mAuth.getCurrentUser();
imageView = rootView.findViewById(R.id.water_image);
capture = rootView.findViewById(R.id.water_capture);
upload = rootView.findViewById(R.id.water_upload);
upload.setEnabled(false);
capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cam_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cam_intent.resolveActivity(getActivity().getPackageManager()) != null){
startActivityForResult(cam_intent, CAM_REQUEST);
}
}
});
mProgress = new ProgressDialog(getContext());
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProgress.setMessage("Uploading Image...");
mProgress.show();
StorageReference filepath = mStorageRef.child("Photos").child(user.getUid()).child("Water").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mProgress.dismiss();
Toast.makeText(getContext(), "Upload Finished...", Toast.LENGTH_LONG).show();
Intent i = new Intent(getContext(), WaterConsumption.class);
startActivity(i);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mProgress.dismiss();
Toast.makeText(getContext(), "Upload has stopped.", Toast.LENGTH_LONG).show();
}
});
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST && data != null){
upload.setEnabled(true);
uri = data.getData();
Bundle extras = data.getExtras();
photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
}
}
错误:
E / AndroidRuntime:致命异常:主要 处理:com.example.akoni.conread,PID:13901 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.net.Uri.getLastPathSegment()' 在com.example.akoni.conread.WaterConsumptionPage $ 2.onClick(WaterConsumptionPage.java:87) 在android.view.View.performClick(View.java:5265) 在android.view.View $ PerformClick.run(View.java:21534) 在android.os.Handler.handleCallback(Handler.java:815) 在android.os.Handler.dispatchMessage(Handler.java:104) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
我还尝试将此代码放在 onActivityResult()中,以测试我是否可以保存图像,但我还有另一个错误(如下所示)。
StorageReference filepath = mStorageRef.child("Photos").child(user.getUid()).child("Water").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mProgress.dismiss();
Toast.makeText(getContext(), "Upload Finished...", Toast.LENGTH_LONG).show();
Intent i = new Intent(getContext(), WaterConsumption.class);
startActivity(i);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mProgress.dismiss();
Toast.makeText(getContext(), "Upload has stopped.", Toast.LENGTH_LONG).show();
}
});
错误:
E / AndroidRuntime:致命异常:主要 处理:com.example.akoni.conread,PID:14526 java.lang.RuntimeException:将结果ResultInfo {who = null,request = 65537,result = -1,data = Intent {act = inline-data(has extras)}}传递给activity {com.example.akoni.conread / com.example.akoni.conread.TabMenu}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.net.Uri.getLastPathSegment()' 在android.app.ActivityThread.deliverResults(ActivityThread.java:3929) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3972) 在android.app.ActivityThread.-wrap16(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1537) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.net.Uri.getLastPathSegment()' 在com.example.akoni.conread.WaterConsumptionPage.onActivityResult(WaterConsumptionPage.java:120) 在android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) 在android.app.Activity.dispatchActivityResult(Activity.java:6502) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3925) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3972) 在android.app.ActivityThread.-wrap16(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1537) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
我想捕获图像并首先将其查看到ImageView ,然后点击upload
将图像保存到firebase。
答案 0 :(得分:0)
你应该删除这行代码:
super.onActivityResult(requestCode, resultCode, data);
因为我认为这一行会导致data.getIntent()没有返回任何内容。
如果可以,您还应该检查resultCode
。
这是一个例子:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
Uri uri = data.getData();
}
}