2016-12-06 3 views
0

誰もがこれがうまくいかない理由についての洞察を持っているかどうかわかります。すべてのコードの最初の方がウェブサイトのpdfへのリンクを取得し、pdfへのリンクは確かに正しいですが、それは私のSDカードにダウンロードされていないようです。それは私のデバイス上のアンドロイドスタジオからそれを実行している方法で完了している場合は?リンクは、ファイル名のみ、全体のURLを与えるためのいくつかの方法を使用してPDFなどへの私のマニフェストsdカードにファイルをダウンロードしていない

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

public class MainActivity extends AppCompatActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button btnFetchData = (Button) findViewById(R.id.buttonTest); 
    btnFetchData.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      new FetchWebsiteData().execute(); 


     } 
    }); 

} 
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> { 

    private String pdfLink = "didnt work"; 
    private Link foundLink = new Link(""); 
    String fileLocation= ""; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 


    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     int count; 
     try { 

      Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get(); 
      //Elements links = doc.select("a[title=\"Download offers in store\"]"); 
      Element links = doc.select("a[title=\"Download offers in store\"]").first(); 
      foundLink = new Link(links.attr("href")); 

      URL url = new URL(foundLink.getUrlWithDomain()); 

      //create the new connection 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      //set up some things on the connection 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 

      //and connect! 
      urlConnection.connect(); 

      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      //create a new file, specifying the path, and the filename 
      //which we want to save the file as. 
      File file = new File(SDCardRoot,foundLink.getFileNameOnly()); 
      fileLocation = file.toString(); 
      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //this will be used in reading the data from the internet 
      InputStream inputStream = urlConnection.getInputStream(); 

      //this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      //variable to store total downloaded bytes 
      int downloadedSize = 0; 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       //add the data in the buffer to the file in the file output stream (the file on the sd card 
       fileOutput.write(buffer, 0, bufferLength); 
       //add up the size so we know how much is downloaded 
       downloadedSize += bufferLength; 
       //this is where you would do something to report the progress, like this maybe 


      } 
      //close the output stream when done 
      fileOutput.close(); 





     } catch (IOException e) { 
      //e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     TextView txttitle = (TextView) findViewById(R.id.resultTextView); 
     //testing the file location 
     txttitle.setText(fileLocation); 

    } 

} 

権限

EDIT:java.io.FileNotFoundExceptionに役立つはずの例外スタックトレースで次のものが見つかりました:/storage/emulated/0/29-11-16.pdf:オープンに失敗しました:EACCES(許可が拒否されました)

+1

ようにそれを使用しますが、エラー・メッセージを取得しましたか? Webサイトからのデータをまったく受け取った場合はログに記録しましたか?まず、データをダウンロードするか、ファイルに書き込むことで問題が発生するかどうかを確認します。 – MrSmith42

+2

'catch(IOException e){ //e.printStackTrace(); } '。スタックトレースをプリントしましょう!また、e.getMessage()を返し、onPostExecuteで検査します。今あなたは何も知らない! – greenapps

+0

'onPostExecute(Void result)'。 doInBoackgroundは常に 'null'を返すので、' result'は常に 'null'です。 – greenapps

答えて

1

Android 6 +あなたは実行時のアクセス許可を求める必要があります。ここでは例

private void checkPermission() { 
    final String permissionWrite = Manifest.permission.WRITE_EXTERNAL_STORAGE; 
    final String permissionRead = Manifest.permission.READ_EXTERNAL_STORAGE; 

    if (ContextCompat.checkSelfPermission(getActivity(), permissionWrite) != PermissionChecker.PERMISSION_GRANTED 
      || ContextCompat.checkSelfPermission(getActivity(), permissionRead) != PermissionChecker.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{permissionWrite, permissionRead}, 0); 
     return; 
    } 
    new FetchWebsiteData().execute(); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    if (requestCode != 0) return; 
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED 
      && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 
     new FetchWebsiteData().execute(); 
    } else { 
     // well no permission granted 
    } 
} 

されており、この

btnFetchData.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     checkPermission(); 
    } 
}); 
+0

実行時アクセス許可を求める前に、私は遭遇していない助けを感謝します。checkPermissionのコードでエラーが発生しました。(ContextCompat.checkSelfPermission(getActivity()はメソッドgetActivityを解決できません) – Daniel

+0

@Daniel Yeahこれは 'Fragment'メソッドです。あなたは' checkSelfPermission(permissionWrite) 'などでアクティビティで直接呼び出すことができます。 –

関連する問題