Android模拟器文件读取权限

时间:2012-07-28 13:31:42

标签: android android-emulator

我需要将我的android模拟器目录('data \ data \ org.mypackage \ file.dat')中的文件上传到远程服务器,但是当我尝试访问该文件时,它会出现错误,如'error:权限被拒绝',我将我的sqlite数据库存储在'data \ data \ org.mypackage \ databases \'文件夹中,在应用程序启动期间如果该文件夹中没有sqlite数据库我将其从我的资产文件夹复制到该目录并访问它从那里它的工作完美,但在案件上传任务,它要求许可为什么会发生这种情况?以下是我的uploadFile方法

    private void uploadFile(){ 

    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 
    String existingFileName="/data/data/org.mypackage/file.dat";
    GeneralFunctions.comment(existingFileName);
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary =  "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String responseFromServer = ""; 
    String urlString = "http://117.231.150.213:8080/upload.jsp"; 
    try 
    { 
     //------------------ CLIENT REQUEST 
    FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) ); 
     // open a URL connection to the Servlet 
     URL url = new URL(urlString); 
     // Open a HTTP connection to the URL 
     conn = (HttpURLConnection) url.openConnection(); 
     // Allow Inputs 
     conn.setDoInput(true); 
     // Allow Outputs 
     conn.setDoOutput(true); 
     // Don't use a cached copy. 
     conn.setUseCaches(false); 
     // Use a post method. 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
     dos = new DataOutputStream( conn.getOutputStream() ); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); 
     dos.writeBytes(lineEnd); 
     // create a buffer of maximum size 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 
     // read file and write it into form... 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     while (bytesRead > 0) 
     { 
      dos.write(buffer, 0, bufferSize); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 
     // send multipart form data necesssary after file data... 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
     // close streams 
     GF.showToast("File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 
    } 
    catch (Exception ex) 
    { 
         GeneralFunctions.exception("error: " + ex.getMessage()); 
    } 

    //------------------ read the SERVER RESPONSE 
    try { 
          inStream = new DataInputStream ( conn.getInputStream() ); 
          String str; 

          while (( str = inStream.readLine()) != null) 
          { 
              GeneralFunctions.comment("Server Response "+str); 
          } 
          inStream.close(); 

    } 
    catch (Exception ioex){ 
          GeneralFunctions.exception("error: " + ioex.getMessage()); 
    } 
  } 

1 个答案:

答案 0 :(得分:2)

为您的清单添加INTERNET权限。

如果你能提供logcat,那么它会有所帮助。