2017-03-24 8 views
0

解決策を見つけようとしましたが、私は2つのクラスを持っていませんでした.1つはsysnctaskクラスで、ビデオファイルをダウンロードしてもうまく動作します。 //コードボタンをクリックしてボタン内のメソッドを呼び出すと、その値が使用されます

public class DownloadFileAsync extends AsyncTask<String, String, String> { 

private Context context; 

public DownloadFileAsync(Context context) 
{ 
    this.context = context; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 
protected String doInBackground(String... f_url) { 
    int count; 
    try { 
     URL url = new URL(f_url[0]); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 

     int lenghtOfFile = conection.getContentLength(); 

     // download the file 
     InputStream input = new BufferedInputStream(url.openStream(), 8192); 
     Dominika_details dominika_details= new Dominika_details(); 
     // Output stream 
     OutputStream output = new FileOutputStream("/sdcard/"+YOUTUBE_ID); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 

      publishProgress(""+(int)((total*100)/lenghtOfFile)); 

      // writing data to file 
      output.write(data, 0, count); 
     } 

     // flushing output 
     output.flush(); 

     // closing streams 
     output.close(); 
     input.close(); 

    } catch (Exception e) { 
     Log.e("Error: ", e.getMessage()); 
    } 

    return null; 
} 

protected void onProgressUpdate(String... progress) { 
    // setting progress percentage 
    } 
@Override 
protected void onPostExecute(String file_url) { 
    // dismiss the dialog after the file was downloaded 
    } 
     } 

第一方がダウンロード形式

//コード

public class GetYoutubeUrl extends AppCompatActivity { 
private static final String YOUTUBE_ID = "3LiubyYpEUk"; 
private final YouTubeExtractor mExtractor = YouTubeExtractor.create(); 
private Callback<YouTubeExtractionResult> mExtractionCallback = new Callback<YouTubeExtractionResult>() { 
    @Override 
    public void onResponse(Call<YouTubeExtractionResult> call, Response<YouTubeExtractionResult> response) { 
     bindVideoResult(response.body()); 
    } 
    @Override 
    public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) { 
     onError(t); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_get_youtube_url); 
    mExtractor.extract(YOUTUBE_ID).enqueue(mExtractionCallback); 


    Button download= (Button)findViewById(R.id.download); 
    download.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 



     } 
    }); 
} 
private void onError(Throwable t) { 
    t.printStackTrace(); 
    Toast.makeText(GetYoutubeUrl.this, "It failed to extract. So sad", Toast.LENGTH_SHORT).show(); 
} 

private void bindVideoResult(YouTubeExtractionResult result) { 
/// Here you can get download url link 
    String video= String.valueOf(result.getSd360VideoUri()); 
    new DownloadFileAsync(this).execute(video); 
} 

にユーチューブ動画IDを変換活性 "GetYoutubeUrl" であります}

GetYoutubeUrlアクティビティのbindVideoResultメソッドでは、asynctaskクラスを呼び出すことができ、入力として "String video"値を使用してビデオを非常に細かくダウンロードできます。 私が達成したいことは二つのものされていますbindVideoResult方法から

  1. GetYoutubeUrlアクティビティでclicklistener上のダウンロードボタンがあるので、どのように私はそれ 内asynctaskクラスを呼び出すことができ、その値を使用し、「ビデオ」というように私ボタンをクリックしたときにのみ、このビデオファイルをダウンロードできます。 2.How私がダウンロードしたファイル名として、すべてのビデオIDを割り当てることができます

?、私はGetYoutubeUrl活動に意味ID「YOUTUBE_IDは」
を導入どこので、どのように私はasyntask」のOutputStream出力に名前を取ることができています=新しいFileOutputStream( "/ sdcard /" + YOUTUBE_ID); "

// logcat

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41ebd9c0) 
    E/AndroidRuntime: FATAL EXCEPTION: main 
       android.view.WindowManager$BadTokenException: Unable to add  window -- token null is not for an application 
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:650) 
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
        at android.app.Dialog.show(Dialog.java:281) 
        at com.fredycom.myyoutube.DownloadFileAsync.onPreExecute(DownloadFileAsync.java:41) 
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 
        at android.os.AsyncTask.execute(AsyncTask.java:534) 
        at com.fredycom.myyoutube.GetYoutubeUrl$2.onClick(GetYoutubeUrl.java:43) 
        at android.view.View.performClick(View.java:4212) 
        at android.view.View$PerformClick.run(View.java:17476) 
        at android.os.Handler.handleCallback(Handler.java:800) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5371) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:525) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
        at dalvik.system.NativeStart.main(Native Method) 

I /プロセス:送信信号。 PID:21771 SIG:9 アプリケーションが終了しました。

答えて

1

この動画ファイルは、ボタンをクリックしたときにのみダウンロードしてください。

グローバル変数としてString videoを宣言します。そして、どのように私は、ダウンロードしたファイル名として、すべてのビデオIDを割り当てることができ、ボタンのonClick()

download.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      new DownloadFileAsync(this).execute(video); 
     } 
    }); 

でAsyncTaskを呼びますか? はAsyncTask引数の引数としてごYOUTUBE_VIDEO_IDを渡す -

public class DownloadFileAsync extends AsyncTask<String, String, String> { 

    private Context context; 
    private String youtubeId; 

    public DownloadFileAsync(Context context, String youtubeId){ 
     this.context = context; 
     this.youtubeId = youtubeId; // Use youtubeId as file name 
    } 

    ..... 
} 

あなたが好きasyncTaskを呼び出すことになる -

new DownloadFileAsync(this, YOUTUBE_VIDEO_ID).execute(video); 
+0

私は新しいDownloadFileAsync(これ、YOUTUBE_ID).excute(ビデオ)を導入しています。クリックリスナーの側にあり、DownloadFileAsync(Mainactivity.this、YOUTUBE_ID).excute(video);のように変更します。このログキャストエラーで閉じるようにしたとき@Wizard –

+0

そして?問題は解決しましたか? – Wizard

+0

@ F.joel皆さんのご質問にerrorcatのコードを追加してください! – Wizard

関連する問題