0

当我使用 android 设备拍摄图像时,我使用的是 Oracle MAF 2.1.0.0.41,我看到这个图像的路径是:file:/storage/emulated/0/Android/data/com.example.app /142416553.jpg

我尝试使用 java bean 类访问此图像:

try {
         Object imageSource = (Object) AdfmfJavaUtilities.evaluateELExpression("#{bindings.Return.inputValue}");
         String path = (String) imageSource;
         path = "***path: " + path; 
        File f = new File(path);

         path = path + f.exists() + f.canRead() + f.canWrite() + " "+imageSource.getClass();
         InputStream is = new BufferedInputStream(new FileInputStream(path));
         InputStreamReader rdr = new InputStreamReader(is, "UTF-8");
         StringBuilder contents = new StringBuilder();
         char[] buff = new char[4096];
         int len = rdr.read(buff);
         while (len >= 0) {
             contents.append(buff, 0, len);
         }
         System.out.println("***Output: " + buff.toString());
         image = buff.toString();
     } catch (FileNotFoundException e) {
         image =" ***Error FileNotFoundException: " + e.getMessage();
     } catch (IOException e) {
         image = "***Error IOException: " + e.getMessage();
     } catch (Exception e) {
         image = "***Error Exception: " + e.getMessage();
     }

但我遇到了这个错误:没有这样的文件或目录不能读、写和不存在。

谁能帮我!如何从缓存目录中读取图像。

4

1 回答 1

0

我已经解决了我的问题 URI uri = new URI(path); 文件 f = new File(uri.getPath());

        byte[] bytes = loadFile(f);
        byte[] encoded = Base64.getEncoder().encode(bytes);

    private  byte[] loadFile(File file) throws IOException {
            InputStream is = new FileInputStream(file);

            long length = file.length();
            if (length > Integer.MAX_VALUE) {
                // File is too large
            }
            byte[] bytes = new byte[(int)length];

            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }

            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "+file.getName());
            }
            is.close();
            return bytes;
        }
于 2015-02-17T13:24:27.113 回答