2013-08-26 2 views
0

Это код для сохранения изображений на SD-карте, если и если не существует. но я не знаю, как его прочитать. Может кто-нибудь мне помочь.сохранить изображение в android

Это метод загрузки файла:

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 = 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(); } 
Смежные вопросы