猿问

尝试通过使用默认相机应用程序并捕获图像并上传到 Firebase 存储来使用 Firebase

我正在尝试通过打开默认相机应用程序并捕获图像并上传到 Firebase 存储来使用 Firebase 与 Android Studio 合作。但是我无法这样做。但是我可以打开相机并单击图像。一旦我点击下一步,我的应用程序就会崩溃。


我已成功连接 Firebase 存储、身份验证和实时数据库。


我想我在代码中遗漏了一些东西。任何帮助,将不胜感激


在此处获取错误消息:


StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());

请检查我的整个代码,如果有任何错误,请告诉我。


这是我的代码:


活动_main.xml :


<Button

    android:id="@+id/btnOpenCamera"

    android:layout_width="252dp"

    android:layout_height="45dp"

    android:layout_marginBottom="8dp"

    android:layout_marginEnd="8dp"

    android:layout_marginStart="8dp"

    android:layout_marginTop="8dp"

    android:text="open camera"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintHorizontal_bias="0.457"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent"

    app:layout_constraintVertical_bias="0.049" />


<ImageView

    android:id="@+id/ivImage"

    android:layout_width="360dp"

    android:layout_height="310dp"

    android:layout_marginBottom="8dp"

    android:layout_marginEnd="8dp"

    android:layout_marginStart="8dp"

    android:layout_marginTop="8dp"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/btnOpenCamera"

    app:layout_constraintVertical_bias="0.272"

    app:srcCompat="@drawable/common_full_open_on_phone" />

主活动.java :


public class MainActivity extends AppCompatActivity {


    private Button openCamera;

    private ImageView image;


    private static final int CAMERA_REQUEST_CODE = 100;


    private StorageReference mStorage;

    private ProgressDialog mProgress;


            }

        });

    }

互换的青春
浏览 133回答 2
2回答

慕妹3146593

尝试这个:@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; Bundle extras = data.getExtras();&nbsp; &nbsp; &nbsp; &nbsp; Bitmap imageBitmap = (Bitmap) extras.get("data");&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream baos = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);&nbsp; &nbsp; &nbsp; &nbsp; byte[] data = baos.toByteArray();&nbsp; &nbsp; &nbsp; &nbsp; filepath.putBytes(data).add....&nbsp; &nbsp; }}在我看来,数据意图包含图像而不是 uri
随时随地看视频慕课网APP

相关分类

Java
我要回答