简体   繁体   English

Android Studio - Uri上传到Firebase

[英]Android Studio - Uri Upload to Firebase

I'm trying to use the camera to take a picture and upload it to Firebase storage.我正在尝试使用相机拍照并将其上传到 Firebase 存储。 My app is connected to Firebase and passes the dependencies checks.我的应用程序连接到 Firebase 并通过了依赖项检查。 The code compiles fine and when I click on my button to open the camera, it opens the camera and lets me take a picture.代码编译正常,当我点击我的按钮打开相机时,它会打开相机并让我拍照。 As soon as I'm done taking the picture and click okay to go back to the activity, it crashes the app.一旦我拍完照片并单击确定以 go 返回活动,应用程序就会崩溃。 I've tried several variations, and all seems to end the same way.我尝试了几种变体,似乎都以相同的方式结束。 I have a front image, back image, and selfie, but I'm only working on the front image so that I can get it to work, then I'll add the back image and selfie picture.我有正面图像、背面图像和自拍,但我只处理正面图像以便让它工作,然后我将添加背面图像和自拍图片。 I have the Firebase storage rules wide open so that anyone can write to it (this is a test environment with no real data).我将 Firebase 存储规则完全开放,以便任何人都可以写入它(这是一个没有真实数据的测试环境)。

Any thoughts on what would be crashing it?关于什么会使它崩溃的任何想法?


import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.firebase.storage.StorageReference;
import com.google.j2objc.annotations.Weak;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;

public class RegisterID extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    public String IDIssuer_Str = "Default";
    ActivityResultLauncher <Intent> activityResultLauncherfront;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_id);

        Spinner IDIssuerSpinner = findViewById(R.id.ID_Issuer_Spin);
        ArrayAdapter<CharSequence> ID_Issuer_adapter = ArrayAdapter.createFromResource(this,
                R.array.ID_Issuer_List, android.R.layout.simple_spinner_item);
        ID_Issuer_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        IDIssuerSpinner.setAdapter(ID_Issuer_adapter);
        IDIssuerSpinner.setOnItemSelectedListener(this);

        //Set variables for the front of the ID
        Button FrontIDImage_Btn = findViewById(R.id.IDFront_Btn);
        ImageView Front_Image_id = findViewById(R.id.IDFront_image);

        //Set variables for the back of the ID
        Button BackIDImage_Btn = findViewById(R.id.IDBack_Btn);
        ImageView Back_Image_id = findViewById(R.id.IDBack_image);

        //Set variables for a selfie button
        Button SelfieIDImage_Btn = findViewById(R.id.Selfie_Btn);
        ImageView Selfie_Image_id = findViewById(R.id.Selfie_image);

        //Set variables for a submit button
        Button SubmitFileToStorage = findViewById(R.id.Submit_IDs_Btn);

        //Actions when the Front ID Button is selected
        FrontIDImage_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View view) {
                Intent front_ID_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                activityResultLauncherfront.launch(front_ID_intent);
            }
        });

        //Launch Camera for Front of ID camera picture
        activityResultLauncherfront = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                    Bundle frontExtras = result.getData().getExtras();
                    Uri frontIDUri;
                    Bitmap bitmap = (Bitmap) frontExtras.get("data");
                    Front_Image_id.setImageBitmap(bitmap);

                    WeakReference<Bitmap> FrontResult=new WeakReference<>(Bitmap.createScaledBitmap(bitmap,
                            bitmap.getWidth(), bitmap.getHeight(), false).copy
                            (Bitmap.Config.RGB_565, true));

                    Bitmap FrontBM=FrontResult.get();
                    frontIDUri = saveFrontImage(FrontBM, RegisterID.this);

                    //Toast.makeText(RegisterID.this, (CharSequence) frontIDUri, Toast.LENGTH_LONG).show();
            }
        });


        SubmitFileToStorage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSubmitIDtoStorage();
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        switch(adapterView.getId()) {
            case (R.id.ID_Issuer_Spin):
                IDIssuer_Str = adapterView.getItemAtPosition(i).toString();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    private Uri saveFrontImage(Bitmap FrontImage, Context context) {

        File imagesFolder=new File(context.getCacheDir(), "images");
        Uri uri=null;

        try {
            imagesFolder.mkdirs();
            File file=new File (imagesFolder, "capture_Front_ID.jpg");
            FileOutputStream stream = new FileOutputStream(file);
            FrontImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            uri= FileProvider.getUriForFile(context.getApplicationContext(), "com.project.MM"+".provider", file);

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return uri;
    }

    public void openSubmitIDtoStorage() {
        Intent intent = new Intent(this, Activity_Home.class);
        startActivity(intent);
    }

}

Updated again with logcat info:再次更新了 logcat 信息:

java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.CharSequence at android.app.ActivityThread.deliverResults(ActivityThread.java:5340) at android.app.ActivityThread.handleSendResult(ActivityThread.java:5379) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.l java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.CharSequence at android.app.ActivityThread.deliverResults(ActivityThread.java:5340) at android.app.ActivityThread.handleSendResult(ActivityThread.java: 5379) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java :135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) at android.os.Handler.dispatchMessage(Handler.java: 106) 在 android.os.Looper.loopOnce(Looper.java:201) 在 android.os.Looper.l oop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7842) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.CharSequence at com.mm.mm.RegisterID$2.onActivityResult(RegisterID.java:93) at com.mm.mm.RegisterID$2.onActivityResult(RegisterID.java:78) at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:418) at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:375) at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:777) at androidx.fra oop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7842) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run( RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.CharSequence at com .mm.mm.RegisterID$2.onActivityResult(RegisterID.java:93) 在 com.mm.mm.RegisterID$2.onActivityResult(RegisterID.java:78) 在 androidx.activity.result.ActivityResultRegistry.doDispatch(Activity86924158Activity864888:78) ) 在 androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:375) 在 androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:777) 在 androidx.fra gment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164) at android.app.Activity.dispatchActivityResult(Activity.java:8385) at android.app.ActivityThread.deliverResults(ActivityThread.java:5333) at android.app.ActivityThread.handleSendResult(ActivityThread.java:5379) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper. gment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164) at android.app.Activity.dispatchActivityResult(Activity.java:8385) at android.app.ActivityThread.deliverResults(ActivityThread.java:5333) at android.app.ActivityThread .handleSendResult(ActivityThread.java:5379) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction. TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) at android.os.Handler .dispatchMessage(Handler.java:106) 在 android.os.Looper.loopOnce(Looper. java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7842) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7842) at java.lang.reflect.Method.invoke(Native Method) at com.android .internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Okay - the provider issue was the real issue.好的 - 提供商问题才是真正的问题。 Once I fixed that, the CharSequence issue was a make.toast issue that I created myself to try and troubleshoot the provider issue.一旦我解决了这个问题,CharSequence 问题就是我自己创建的一个 make.toast 问题,用于尝试解决提供程序问题。 So once I fixed both, no more crashes.因此,一旦我修复了两者,就不会再发生崩溃。 It's still not writing the image file to Firebase, but that's for another thread if I can't get it working on my own.它仍然没有将图像文件写入 Firebase,但如果我不能让它自己工作,那是另一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM