2014-10-30 4 views
1

apk 파일을 다운로드하고 설치하려고합니다.
외부 저장소 디렉터리로 처리했지만 로컬 디렉터리에서 파일을 다운로드 할 때이를 구문 분석 할 수 없습니다. 여기 내부 저장소 파일에서 앱을 설치하는 방법

는 DownloadTask는 AsyncTask를으로부터 연장되는 클래스에서 OnCreate 방법

final DownloadTask downloadTask = new DownloadTask(this);  
downloadTask.execute("http://file.appsapk.com/wp-content/uploads/apps-2/Gmail.apk","Gmail.apk"); 

의 코드이다. '

@Override 
    protected void onPostExecute(String result) { 
     mWakeLock.release(); 
     mProgressDialog.dismiss(); 
     if (result != null) 
     { 
      Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show(); 
      File file = new File(directory, file_name); 
      Intent promptInstall = new Intent(Intent.ACTION_VIEW) 
       .setDataAndType(Uri.fromFile(file), 
         "application/vnd.android.package-archive"); 
      context.startActivity(promptInstall); 
      finish(); 

     } 

그것은 외부 저장과 완벽하게 실행하지만 원 : 여기 는 백그라운드 작업입니다 :

@Override 
    protected String doInBackground(String... sUrl) { 
     file_name=sUrl[1]; 
     Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
     if (isSDPresent) { 
      directory = new File(Environment.getExternalStorageDirectory()+File.separator+"app_directory"); 
     } 
     else 
     { 
      directory = getFilesDir(); 

     } 
     if (!directory.exists()) 
      directory.mkdirs(); 
     InputStream input = null; 
     OutputStream output = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      int fileLength = connection.getContentLength(); 
      input = connection.getInputStream(); 
      output = new FileOutputStream(directory+"/"+file_name); 
      byte[] buffer = new byte[1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(buffer)) != -1) { 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 
       if (fileLength > 0) // only if total length is known 
        publishProgress((int) (total * 100/fileLength)); 
       output.write(buffer, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (input != null) 
        input.close(); 

       if (output != null) 
        output.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 

그리고이 파일을 다운로드 할 첫 번째 후 실행 후 실행 방법 외장과 함께 달린다. 왜 안돼?

+0

어디에서 응용 프로그램을 테스트합니까? 에뮬레이터 또는 실제 장치에서? – chkm8

+0

에뮬레이터에서 – Safouen

+0

보십시오. http://stackoverflow.com/questions/4967669/android-install-apk-programmatically 비슷한 문제가 있습니다. – chkm8

답변

1

안드로이드 응용 프로그램이 PRIVATE 모드로 작성된 경우 다른 응용 프로그램 파일에서 읽을 수 없기 때문에 발생합니다. 따라서 파일 모드를 변경해야합니다. 아래의 onPostExecute() 메소드의 else 부분 만 수정하면됩니다.

try { 
     String tempfile="xyzfile"; 
     File file = new File(directory, file_name); 
     FileInputStream inStream = new FileInputStream(file); 
     FileOutputStream fos = context.openFileOutput(tempfile, 
       context.MODE_WORLD_WRITEABLE | context.MODE_WORLD_READABLE); 

     byte[] buffer = new byte[1024]; 
     int length; 
     // copy the file content in bytes 
     while ((length = inStream.read(buffer)) > 0) { 

      fos.write(buffer, 0, length); 

     } 
     inStream.close(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    File file = new File(context.getFilesDir(), tempfile); 
    Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(
      Uri.fromFile(file), "application/vnd.android.package-archive"); 
    context.startActivity(promptInstall); 
관련 문제