无法在android studio中看到图像视图

时间:2018-05-18 17:42:14

标签: java android firebase

我正致力于将图像发送到firebase。它首先选择一个图像,然后显示图像,最后你可以点击将图像发送到firebase。我有它的工作,但在我发送图像之前,我无法看到图像的预览。

我想让它显示我在图像视图中选择的图像。

感谢。

这是我的布局。

public class UploadImage extends AppCompatActivity {

private Button btnChoose;
private Button btnUpload;
private ImageView imageView;
private Uri filePath;
FirebaseStorage storage;
StorageReference storageReference;

private final int PICK_IMAGE_REQUEST = 71;



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

    btnChoose = findViewById(R.id.btnChoose);
    btnUpload = findViewById(R.id.btnUpload);
    imageView = findViewById(R.id.imgView);
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();
    btnChoose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chooseImage();
        }
    });
    btnUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            uploadImage();
        }
    });
        }
        private void chooseImage()  {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select 
Picture"), PICK_IMAGE_REQUEST);
        }
        private void uploadImage()  {
            if(filePath != null)
            {
                final ProgressDialog progressDialog = new 
ProgressDialog(this);
                progressDialog.setTitle("Uploading...");
                progressDialog.show();

                StorageReference ref = storageReference.child("images/"+ 
UUID.randomUUID().toString());
                ref.putFile(filePath)
                        .addOnSuccessListener(new 
OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot 
 taskSnapshot) {
                                progressDialog.dismiss();
                                Toast.makeText(UploadImage.this, "Uploaded", 
Toast.LENGTH_SHORT).show();
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                progressDialog.dismiss();
                                Toast.makeText(UploadImage.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                        .getTotalByteCount());
                                progressDialog.setMessage("Uploaded "+(int)progress+"%");
                            }
                        });
            }
        }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null )
    {
        filePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

这是班级。

Object[]

}

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/layout_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnChoose"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Choose" />

        <Button
            android:id="@+id/btnUpload"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Upload" />
    </LinearLayout>


    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

<强>更新

我在您的代码段中看到了选择图片的低级代码。在使用之前,您甚至没有压缩图像。

我建议您使用一些库来选择图像,因为它们已经与android api版本,权限等兼容。

ImagePicker imagePicker = new ImagePicker(Activity.this);
imagePicker.setImagePickerCallback(new ImagePickerCallback(){
        @Override
        public void onImagesChosen(List<ChosenImage> images) {
            // Display images
        }

        @Override
        public void onError(String message) {
            // Do error handling
        }
    }
);
// imagePicker.allowMultiple(); // Default is false
// imagePicker.shouldGenerateMetadata(false); // Default is true
// imagePicker.shouldGenerateThumbnails(false); // Default is true
imagePicker.pickImage();

只需将compile 'com.kbeanie:multipicker:1.1.31@aar'添加到您的应用级build.gradle

即可