【问题标题】:How to set Image on ImageButton dynamically?如何在 ImageButton 上动态设置 Image?
【发布时间】:2015-06-11 19:45:14
【问题描述】:

我想在我的应用程序中的按钮上设置图像,从 sdcard 上的文件动态设置。我已经尝试过这段代码,但它不起作用。我试图将图像转换为位图对象并将该对象设置为 ImageButton,但它没有显示任何内容。我该如何解决这个问题?

我的代码:

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

    File  imageFile = new File("/sdcard0/DCIM/camera/jbn.jpg");
    Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

    ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
    button1.setImageBitmap(bmp);
}

   XML 

   <ImageButton
   android:layout_width="200dip"
   android:layout_height="200dip"
   android:id="@+id/imgBtn"
   />

算法

void loadPic()
  {
      String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
      String pathName = baseDir + "/DCIM/camera/";
      File parentDir=new File(pathName);

      File[] files = parentDir.listFiles();
      Date lastDate;
      String lastFileName;
      boolean isFirstFile = true; //just temp variable for being sure that we are on the first file
      for (File file : files) {
          if(isFirstFile){
              lastDate = new Date(file.lastModified());
              isFirstFile = false;
          }
          if(file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")){
              Date lastModDate = new Date(file.lastModified());
              if (lastModDate.after(lastDate))  {
                  lastDate = lastModDate;
                  lastFileName = file.getName();
              }
          }
      }

【问题讨论】:

  • 看看这个库对我帮助很大...square.github.io/picasso
  • 你能发布你的布局文件吗?你的jpg大小是多少?您是否添加了从文件系统读取文件的权限?
  • @user3431672:我已添加读取权限,请查看我的编辑

标签: android android-bitmap android-imagebutton


【解决方案1】:

尝试像这样简单的东西,例如:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "jbn.jpg";
String pathName = baseDir + "/your/folder(s)/" +_ fileName; //maybe your folders are /DCIM/camera/

Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
button1.setImageBitmap(bmp);

【讨论】:

  • 您可以使用以下算法: 1.从相机文件夹中获取所有文件; 2.获取文件的创建日期,如下所示:stackoverflow.com/questions/2389225/… 和 3.获取最新文件作为资源
  • 一会儿,我给你写个简单的例子
  • 这就是我所说的:pastebin.com/TMWAqDd1 我认为您应该遍历目录中的所有文件以查看哪个是最新文件,因为不确定文件在按字母顺序。附:我刚刚看到我在第 13 行犯了一个错误 -> 它是 file.lastModified() with ()
  • 这行代码是否需要修改 if (lastModDate.after(strDate)) line numbr 18 ,strDate 函数不起作用,它显示错误。非常感谢你的努力你已经采取了。
  • 哦,对不起.. 这个if (lastModDate.after(strDate)) { 应该是if (lastModDate.after(lastDate)) { 这是我的错误。再次抱歉。试试这个方法
【解决方案2】:

尝试获取图片文件的绝对路径:

File  imageFile = new File("/sdcard0/DCIM/camera/jibin.jpg");
Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

【讨论】:

    【解决方案3】:

    你可以试试这样的 -

        String path = Environment.getExternalStorageDirectory()
                + "/Images/test.jpg";
    
        File imgFile = new File(path);
        if (imgFile.exists()) {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                    .getAbsolutePath());
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(myBitmap);
        }
    

    【讨论】:

      【解决方案4】:

      如果您想从您拥有的任何 URL 动态设置图像,请以这种方式设置图像。您还可以设置位图的宽度和高度。

      private class LoadImage extends AsyncTask<Bundle, String, Bitmap> {
          Bitmap bitmap;
          @Override
          protected Bitmap doInBackground(Bundle... args) {
              extras=args[0];
              try {
                  InputStream in = new java.net.URL("Enter your URL").openStream();
                  bitmap = BitmapFactory.decodeStream(in);
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return bitmap;
          }
      
          protected void onPostExecute(Bitmap image) {
               imageButton.setImageBitmap(image);      
          }
      }
      

      如果您想从本地目录或资源文件夹中设置图像,那么只需从文件夹中获取图像并将其设置在图像中,您不需要将其转换为位图。 谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多