android 使用activityResultLauncher上传图像,将URI转换为位图

tcomlyy6  于 2023-02-06  发布在  Android
关注(0)|答案(1)|浏览(175)

我是个新手,我试着把这些碎片拼在一起,这样我就能得到我想要的结果。我真的不明白我做错了什么。问题是当我试着把一张个人资料照片放在图片视图中时,当我按下选中的照片时,应用程序崩溃了,我收到了一条类似“URI不能为空”的消息。

public class Profile extends AppCompatActivity {

private ImageView ProfileImg;

private String Tag;

private Uri imagePath = null;

private Uri imageUriPath;

ProgressBar progressBar;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    progressBar = findViewById(R.id.progressBar);
    Button btnUpload = findViewById(R.id.btnUploadPhoto);
    Button btnLogOut = findViewById(R.id.btnLogOut);
    ProfileImg = findViewById(R.id.profile_img);

    /* START ACTIVITY FOR RESULT*/


    btnLogOut.setOnClickListener(view -> {

        FirebaseAuth.getInstance().signOut();
        startActivity(new Intent(Profile.this,MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP));
        finish();

    });

    ProfileImg.setOnClickListener(view -> {

        Intent photoIntent = new Intent(Intent.ACTION_PICK);
        photoIntent.setType("image/*");
        NewStartActivityForResult.launch(photoIntent);

    });
    
    btnUpload.setOnClickListener(view -> {
        
        uploadImage();
        
    });


    /*Select photo from device*/

}

private ActivityResultLauncher<Intent> NewStartActivityForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {

    if (result.getResultCode() == Activity.RESULT_OK){

        Intent GotResults = result.getData();

        if(GotResults == null)
        {
            Log.d(Tag,"InitializedActivityLaunchers: intent data is null");
            return;
        }
        Uri imageUriPath = GotResults.getData();
        GetImageInView();
    }

});

private void uploadImage() {

    FirebaseStorage.getInstance().getReference("images/" + UUID.randomUUID().toString()).putFile(imagePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
            if (task.isSuccessful())
            {
                Toast.makeText(Profile.this, "Image Uploaded!",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(Profile.this, "Something went Wrong!", Toast.LENGTH_SHORT).show();
            }
        }
    });

}

private void GetImageInView(){

    Bitmap bitmap = null;
    try{
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUriPath);
    }catch (IOException e){
        e.printStackTrace();
    }

}

我第一次有一个问题startOnActivityResult其中我有一行通过命令说,这是不赞成和MediaStore.Images.Media.getBitmap其中我有同样的“错误”,不赞成对getBitmap,我试图使用新的方法观看教程和其他论坛,但我认为我不能连接activityResultLauncher与位图,因为activityResultLauncher的输出是意图和位图需要URI。

bz4sfanl

bz4sfanl1#

NewStartActivityForResult中,只需按如下所述管理代码

Uri imageUriPath = GotResults.getData();

imageUriPath = GotResults.getData();

您正在声明并初始化变量,而不是仅初始化

相关问题