내가하려는 것은 이미지를 캡처하여 이미지보기에서보고이를 파이어베이스 저장소에 저장하는 것입니다. 캡처 후 이미지 업로드 - Android
내 코드 : 는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);
}
}
오류 : 나는 또한 내가 이미지를 저장할 수 있는지 테스트하는)합니다 (하여 onActivityResult에서 내부 아래에이 코드를 퍼팅 시도
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.akoni.conread, PID: 13901 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference at com.example.akoni.conread.WaterConsumptionPage$2.onClick(WaterConsumptionPage.java:87) at android.view.View.performClick(View.java:5265) at android.view.View$PerformClick.run(View.java:21534) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
,하지만, 난 여전히 가지고 또 다른 오류 (아래).
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: FATAL EXCEPTION: main Process: com.example.akoni.conread, PID: 14526 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.akoni.conread/com.example.akoni.conread.TabMenu}: 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:3929) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3972) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1537) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference at com.example.akoni.conread.WaterConsumptionPage.onActivityResult(WaterConsumptionPage.java:120) at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) at android.app.Activity.dispatchActivityResult(Activity.java:6502) at android.app.ActivityThread.deliverResults(ActivityThread.java:3925) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3972) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1537) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
나는 capture the image and view it to the imageView first
을 원하고 중포 기지에 이미지를 저장 upload
을 클릭합니다.
도움 주셔서 감사합니다. 나는이 라인은 data.getIntent()는 아무 것도 반환하지 유발하는 것이라고 생각하기 때문에
super.onActivityResult(requestCode, resultCode, data);
:
동일한 코드에서 많은 오류를 덤프하는 대신 한 번에 하나씩 문제를 해결해야합니다. Firebase 저장소와 관련된 코드를 제거하고 사진을 먼저 관리해야합니다 (전체 이미지를 업로드하려는 경우). 이 문제를 처리하는 방법에 대한 공식 자습서가 https://developer.android.com/training/camera/photobasics.html에 있습니다. 이 부분을 해결 한 후에는 질문을 편집하고 업데이트 된 코드를 추가 한 다음 Firebase 코드와 업로드 문제를 추가하십시오. – Luksprog