从Android应用程序缓存中获取捕获的图像并显示为字符串生成器在Oracle MAF中

时间:2015-02-17 10:04:13

标签: android oracle oracle-maf

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

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

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();
     }

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

任何人都可以帮助我!如何从缓存目录中读取图像。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题                 URI uri =新URI(路径);                 文件f =新文件(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;
        }