2013-08-26 3 views
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; 
     } 
+0

이 코드를 사용하십시오. http://stackoverflow.com/a/7887114/964741 –

답변

0

이 시도,

열린 우리당 URI = Uri.parse :

은 다운로드 파일 방법이다 ("파일 : ///sdcard/temporary_file.jpg");

img.setImageURI (uri);

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(); } 
관련 문제