我是第一次使用Firebase,我想用手机图库中的任何图片填充ImageView,但是每次运行该程序时,它都会崩溃。单击Floating Action Button会弹出一个弹出窗口,其中包含两个编辑文本和一个大图像视图,需要在Firebase上上传图像。该想法是,单击ImageView可以直接将您带到Gallery中进行选择。 代码工作正常,直到我尝试从图库中插入图片为止。 imageUserDesc是问题所在,我无法弄清楚自己做错了什么。
MainActivity.java
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
Dialog addPost;
ProgressBar progressBar;
ImageView imageUserDP, imageUserDesc, imageUserBtn;
EditText userTitle, userDesc;
Uri descriptionImageURI = null;
//Button addImage;
private static final int requestCode = 2;
private static final int openGallery = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
progressBar = findViewById(R.id.pop_up_progress);
Toolbar toolbar = findViewById(R.id.toolbar);
imageUserDesc = findViewById(R.id.pop_up_description_image);
//addImage = findViewById(R.id.pop_up_add_image);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popUp();
}
});
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
UpdateHeader();
imageUserDesc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkselfPermission();
}
});
}
//imageUserDesc = (ImageView)findViewById(R.id.pop_up_description_image);
private void openGallery() {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, openGallery);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_OK && resultCode == openGallery && data != null) {
descriptionImageURI = data.getData();
}
}
private void checkselfPermission() {
if(ContextCompat.checkSelfPermission(Home.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(Home.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Toast.makeText(Home.this, "Grant Access Please", Toast.LENGTH_SHORT).show();
}
else {
ActivityCompat.requestPermissions(Home.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
}
}
else {
openGallery();
}
}
private void popUp() {
addPost = new Dialog(this);
addPost.setContentView(R.layout.pop_up_post);
addPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
addPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.WRAP_CONTENT);
addPost.getWindow().getAttributes().gravity = Gravity.TOP;
addPost.show();
imageUserDP = addPost.findViewById(R.id.pop_up_image);
imageUserDesc = addPost.findViewById(R.id.pop_up_description_image);
imageUserBtn = addPost.findViewById(R.id.pop_up_button);
userTitle = addPost.findViewById(R.id.pop_up_title);
userDesc = addPost.findViewById(R.id.pop_up_description);
progressBar = addPost.findViewById(R.id.pop_up_progress);
imageUserBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
imageUserBtn.setVisibility(View.INVISIBLE);
}
});
Glide.with(this).load(currentUser.getPhotoUrl()).into(imageUserDP);
}
XML
<ImageView
android:background="#2d2d2d"
android:id="@+id/pop_up_description_image"
android:layout_width="0dp"
android:layout_height="225dp"
android:layout_marginTop="107dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
答案 0 :(得分:0)
我一直想出这一次哪里出错了。 应该从popUp()方法内部单击imageUserDesc,因为根据我的理解,所有Dialog小部件都在其中声明了。 我们需要做的就是将以下内容添加到方法中:
private void popUp(){
addPost = new Dialog(this);
addPost.setContentView(R.layout.pop_up_post);
addPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
addPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.WRAP_CONTENT);
addPost.getWindow().getAttributes().gravity = Gravity.TOP;
addPost.show();
imageUserDP = addPost.findViewById(R.id.pop_up_image);
imageUserDesc = addPost.findViewById(R.id.pop_up_description_image);
imageUserBtn = addPost.findViewById(R.id.pop_up_button);
userTitle = addPost.findViewById(R.id.pop_up_title);
userDesc = addPost.findViewById(R.id.pop_up_description);
progressBar = addPost.findViewById(R.id.pop_up_progress);
imageUserBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
imageUserBtn.setVisibility(View.INVISIBLE);
}
});
Glide.with(this).load(currentUser.getPhotoUrl()).into(imageUserDP);
setDescImage();
}
private void setDescImage() {
imageUserDesc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkselfPermission();
}
});
}