0

这是用于将图像保存在 SD 卡中(如果不存在)的代码。但我不知道怎么读。任何人都可以帮助我吗?

这是下载文件的方法:

public static String DownLoadFile(String netUrl,  String name ) {

          try {
                    //need uses permission WRITE_EXTERNAL_STORAGE

                ByteArrayBuffer baf = null;
                long startTime = 0;

                //get to directory (a File object) from SD Card
                    File savePath=new File(Environment.getExternalStorageDirectory().getPath()+"/postImages/");
                    String ext="jpg";
                URL url = new URL(netUrl);


                //create your specific file for image storage:
                File file = new File(savePath, name + "." + ext);
                boolean success = true;
                if (!savePath.exists()) {
                    success = savePath.mkdir();
                }
                if (success) {
                    if(file.createNewFile())
                      {
                        file.createNewFile();


                    //write the Bitmap
                    Log.i("file existence", "file does not exist!!!!!!!!!!!");
           /* Open a connection to that URL. */
                URLConnection ucon = url.openConnection();

                InputStream is = ucon.getInputStream();

                BufferedInputStream bis = new BufferedInputStream(is);
                startTime = System.currentTimeMillis();


                baf = new ByteArrayBuffer(5000);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }



                   /* Convert the Bytes read to a String. */
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baf.toByteArray());
                fos.flush();
                fos.close();
                Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

                return file.getAbsolutePath();
                      }//end of create file if not exists

                }//end of if success


            } catch (Exception exx) {
                if (exx.getMessage() != null) {


                } else {


                }

            }

            return null;
        }
4

2 回答 2

0

尝试这个,

uri uri = Uri.parse("file:///sdcard/temporary_file.jpg");

img.setImageURI(uri);

于 2013-08-26T12:50:53.320 回答
0
  if u have image uri so get path from uri like
 String Path = fileUri.getPath();   

  // read file from sdcard       

  public static byte[] readFromStream(String path) throws Exception { File
  file = new File(path); InputStream inputStream = new
  FileInputStream(file); ByteArrayOutputStream baos = new
  ByteArrayOutputStream(); DataOutputStream dos = new
  DataOutputStream(baos); byte[] data = new byte[(int) file.length()]; int
  count = inputStream.read(data); while (count != -1) { dos.write(data, 0,
  count); count = inputStream.read(data); } return baos.toByteArray(); }
于 2013-08-26T13:24:02.460 回答