【问题标题】:Bitmap display using file path使用文件路径显示位图
【发布时间】:2018-04-25 09:01:57
【问题描述】:

我有一些图片网址需要下载并存储在内部存储中。

图片下载保存代码为:

   public void callDownloadImageAsync(int pos) {
        String baseDir = this.getFilesDir().getAbsolutePath() + "/";
        if (allImagesArr.size() > pos) {
            try {
                int count;
                String imageUrl = allImagesArr.get(pos);

                URL url = new URL(imageUrl);
                Utility.printMessage("imageUrl..." + imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(60000);
                connection.setConnectTimeout(60000);
                connection.setDoOutput(true);
                connection.setRequestMethod("GET");
                if (Utility.getToken(this).length() > 0) {
                    Utility.printMessage("@@@ my header for photos " + Utility.getToken(this));
                    try {
                        connection.setRequestProperty("X-CSRF-TOKEN", Utility.getToken(this));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                connection.connect();
                String PATH;
                File imgFile;

//                if (allImagesFileNameArr.get(pos).startsWith("lead_")) {
                if (allImagesFileNameArr.get(pos).startsWith("/")) {
                    imgFile = new File(allImagesFileNameArr.get(pos));
                    PATH = imgFile.getAbsolutePath();
                } else {
                    File folder = new File(baseDir + allImagesFileNameArr.get(pos)).getParentFile();
                    if (!folder.exists())
                        folder.mkdirs();
                    String imageName = allImagesFileNameArr.get(pos);
                    imageName = imageName.substring(imageName.lastIndexOf("/"));

                    imgFile = new File(folder, imageName);
                    PATH = imgFile.getAbsolutePath();
                }

//                }

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(PATH);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    //                    publishProgress ((int)(total*100/lenghtOfFile));
                    output.write(data, 0, count);
                }
//                Bitmap bitmap = BitmapFactory.decodeStream(input);
//                Utility.saveToInternalStorage(context, bitmap, PATH);

                output.flush();
                output.close();
                input.close();

                Utility.printMessage("image saved...");
            } catch (IOException e) {
                e.printStackTrace();
                Utility.logException(e);
            }

            callDownloadImageAsync(pos + 1);
        }
    }

我在日志中收到消息“图像已保存”。 但是当我尝试使用路径读取位图时,出现以下异常:

java.lang.IllegalArgumentException: File /data/user/0/com.app.los_pdapp/files/lead_1/PRAPPL_profile.jpg contains a path separator
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.app.ContextImpl.makeFilename(ContextImpl.java:2413)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.app.ContextImpl.openFileInput(ContextImpl.java:504)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:193)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Util.Utility.readBitmap(Utility.java:977)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Fragments.PD.Applicant.DetailView.bindView(DetailView.java:154)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Fragments.PD.Applicant.DetailView.onViewCreated(DetailView.java:65)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)

我读取位图的方法是:

public static Bitmap readBitmap(Context context, String name) {

        Utility.printMessage("name of file..."+name);


        String name1 = name.substring(name.lastIndexOf("/")+1);
        Utility.printMessage("name of file..."+name1);
        Bitmap b = BitmapFactory.decodeFile(name);
        Utility.printMessage("*************** : " + b);
        if (b != null)
            return b;
        try {
            FileInputStream fis = context.openFileInput(name);
//            FileOutputStream fos = new FileOutputStream(name);
            b = BitmapFactory.decodeStream(fis);
            fis.close();
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            Utility.logException(e);
        }
        return null;
    }

我尝试使用 url 连接流并解码位图,但此解决方案不起作用。 因此,任何人都可以建议上面的代码有什么问题吗?

【问题讨论】:

标签: android file android-bitmap


【解决方案1】:

context.openFileInput(name); 不接受路径,只接受文件名。请改用FileInputStream fis = context.openFileInput(new File(name).getName())

【讨论】:

  • 之前你说我们不能使用 context.openFileInput?
  • 没有getFileName()方法
  • 意思是 getName()
  • openFileInput 仅接受文件名,但由于您的文件位于应用程序的私有存储中,因此这是访问它的唯一方法。如果文件在其他地方,您可以使用FileInputStream 方法
  • 好吧,让我试试
猜你喜欢
  • 1970-01-01
  • 2015-11-30
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2016-05-29
  • 2016-03-09
相关资源
最近更新 更多