【问题标题】:Display imageview onclick and post without imageview if empty如果为空,则显示 imageview onclick 并在没有 imageview 的情况下发布
【发布时间】:2019-12-09 18:12:09
【问题描述】:

我正在为一个博客应用程序编写代码,该应用程序将只发布文本或在其下方发布文本和图像。我在布局中定义了一个 editText 和一个 imageView。我想要那个,除非我点击画廊按钮,否则图像视图不会出现。

我是有意尝试的,但添加活动会使应用程序过于繁重。

 private ImageButton newPostImgbtn;
 private ImageView newPostImage;
 private EditText newPostDesc;
 private Button newPostBtn;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.HomeTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    newPostImage = findViewById(R.id.imageView);
    newPostImgbtn = findViewById(R.id.imageButton2);
    newPostDesc = findViewById(R.id.editText);
    newPostBtn = findViewById(R.id.button2);
}

我通过在图库 newimagebtn 的按钮上放置一个 onclick 侦听器创建了新的活动 imageactivity,仅用于添加图像,但我不想在另一个活动中执行此操作。

public void post(View v) { Intent intent = new Intent(postActivity.this, 
imageActivity.class); startActivity(intent);}

此外,如果用户单击图库按钮,但未选择任何图像,则当单击发布按钮时,仅显示文本而没有空的图像视图区域或错误,因为该字段为空。请帮帮我。

【问题讨论】:

  • 请多分享一些代码,理解不够
  • 我是故意这样做的,我应该为此共享代码吗?我不明白这个问题的代码没有意图。
  • 是的,请分享。
  • 我创建了新的活动 imageactivity,通过在画廊 newimagebtn 的按钮上放置一个 onclick 侦听器来添加图像,但我不想在另一个活动中这样做。代码 > public void post(View v) { Intent intent = new Intent(postActivity.this, imageActivity.class);开始活动(意图);
  • 然后只需使用 finish() ,它将支持您之前的活动

标签: java android android-layout android-intent


【解决方案1】:

首先,您必须获得用户的许可并打开图库意图。当您找到图像 url 时,您需要使用 glide 之类的图像加载器库来显示它。单击按钮时,您需要做的最后一件事是检查图像 url 是否为空。如果您需要进一步的帮助,请告诉我。

【讨论】:

【解决方案2】:

在onCreate方法中添加

newPostImgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getImageFromAlbum();
            }
        });

在你的活动课上

private void getImageFromAlbum(){
    try{
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }catch(Exception exp){
        Log.i("Error",exp.toString());
    }
}

将此方法添加到您的活动类中

    Override
        protected void onActivityResult(int reqCode, int resultCode, Intent data) {
            super.onActivityResult(reqCode, resultCode, data);


            if (resultCode == RESULT_OK) {
                try {
                    final Uri imageUri = data.getData();
                    final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                    final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    newPostImage .setImageBitmap(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
                }

            }else {
//image not selected so do what you want when image is not selected      
}

【讨论】:

  • 如何将此图像添加到 Imageview 'newPostImage'?我没有在 androidstudio 中练习图像,我究竟在哪里学习它?
  • 另外,非常感谢您的回答。我投了赞成票,但我只有 6 个声望,所以我的投票没有显示。
  • 我已经更新了答案,现在它将在您的图像视图上设置图像。如果可行,请接受答案。谢谢
  • 请创建一个全局变量 public static int RESULT_LOAD_IMAGE =23;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多