【问题标题】:Get arraylist of image file object from arraylist of url in android从android中url的arraylist获取图像文件对象的arraylist
【发布时间】:2017-06-29 13:04:46
【问题描述】:

我的目标是:

当它转换为文件对象时,我应该在 imageview 中显示图像路径。

我正在尝试一些具有不同文件对象的解决方案:

File f=null;
    URL url = new URL(info4Model.getExternal_photos().get(position).getImage_path());
        try {
         //   f = new File(url.toURI());//java.lang.IllegalArgumentException: Expected file scheme in URI:
            //f = new File(url.getPath());//  not showing file object in imageview
            //f = new File(url.getFile());// not showing file object in imageview
            f = getFileFromBitmap(info4Model.getExternal_photos().get(position).getImage_path());
             //android.os.NetworkOnMainThreadException , AsyncTask option left only?

        } catch (Exception e) {


        }
///////////////////////////////////
public static Bitmap getBitmapFromURL(String url) {
    try {
        URL url1 = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmapFrmUrl = BitmapFactory.decodeStream(input);
        return bitmapFrmUrl;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}
File getFileFromBitmap(String url) {
    Bitmap bmp = getBitmapFromURL(url);

    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat("MMddhhmmss");
    String dateAsString = simpleDateFormat.format(new Date());
    File filesDir = getActivity().getFilesDir();
    File imageFile = new File(filesDir, dateAsString + ".png");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return imageFile;
}

这里所有的文件对象都在声明后的cmets中显示失败结果。

【问题讨论】:

    标签: android arraylist android-bitmap android-file


    【解决方案1】:

    如果你从 api 获取图片 url..

    那么, 阿联酋毕加索用于将您的图像加载到 imageview 中,例如.. Picasso.with(activityname.this)

       .load("您的图片网址在这里")

       .into(imageView);

    让 api 加载您的图像路径.. 你必须使用多部分..

    为此..

    文件文件=新文件(你的图像路径); FileBody a1=new FileBody(file);

    MultipartEntity mpEntity = new MultipartEntity(); mpEntity.addPart("参数名称", a1);

    希望你能得到帮助。

    【讨论】:

      【解决方案2】:
      public class FileModel {
          File Url;
          File path;
          File file;
          File bitMap;
      
          public File getUrl() {
              return Url;
          }
      
          public void setUrl(File url) {
              Url = url;
          }
      
          public File getPath() {
              return path;
          }
      
          public void setPath(File path) {
              this.path = path;
          }
      
          public File getFile() {
              return file;
          }
      
          public void setFile(File file) {
              this.file = file;
          }
      
          public File getBitMap() {
              return bitMap;
          }
      
          public void setBitMap(File bitMap) {
              this.bitMap = bitMap;
          }
          }  
      
        ArrayList<FileModel> fileArrayList = new ArrayList<>();
      
      FileModel fileModel = new FileModel();
              File f=null;
              URL url = new URL(info4Model.getExternal_photos().get(position).getImage_path());
              try {
                  f = new File(url.toURI());//java.lang.IllegalArgumentException: Expected file scheme in URI:
                  fileModel.setUrl(f);
                  f = new File(url.getPath());//  not showing file object in imageview
                  fileModel.setPath(f);
                  f = new File(url.getFile());// not showing file object in imageview
                  fileModel.setFile(f);
                  f = getFileFromBitmap(info4Model.getExternal_photos().get(position).getImage_path());
                  fileModel.setBitMap(f);
                  //android.os.NetworkOnMainThreadException , AsyncTask option left only?
      
              } catch (Exception e) {
      
                  e.printStackTrace();
              }
      
              fileArrayList.add(fileModel);
      

      文件数组列表终于可以使用了。

      希望对你有帮助,,,!

      【讨论】:

      • 谢谢,但正如您所见,所有对象都有评论结果。所以我的问题是从所有这些中得到的,没有一个 File 对象在工作。
      • 您需要创建一个模型类,然后将模型中的所有值和模型中的所有值放入 ArrayList ...!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2014-11-21
      • 2016-06-22
      • 1970-01-01
      • 2015-04-18
      相关资源
      最近更新 更多