When downloading video files on Android

Asked 2 years ago, Updated 2 years ago, 68 views

I am currently developing an Android app in java language.
This is about interaction with the server.
I want to download the video file that I left on the server through the app to my Android device using httpURLconnection, but the following program only downloads text.
Please give me a hint on how I can program to download video files.
By the way, the server is php

// HTTP communication
public class HttpExextends Activity   
    implements View.OnClickListener {
    private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
    private final static int MP = LinearLayout.LayoutParams.MATCH_PARENT;
    private final static String TAG_READ="read";
    privateEditTexteditText;
    private String text;
    private handler handler = new handler();
    
    // Specifying the URL of the text file (1)
    private final static String URL =
        "http://localhost/test/img01.jpg";
        
    // Called when activities start
    @ Override
    public void onCreate (Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Generate Layout
        LinearLayout layout = new LinearLayout(this);
        layout.setBackgroundColor (Color.WHITE);
        layout.setOrientation (LinearLayout.VERTICAL);
        setContentView (layout);      
        
        // Generate Edit Text
        editText = new EditText(this);
        editText.setText("");
        editText.setLayoutParams(new LinearLayout.LayoutParams(MP,WC));
        layout.addView (editText);

        // Generating Buttons
        layout.addView(makeButton("HTTP Communications", TAG_READ));
    }   

    // Generating Buttons
    private Button makeButton (String text, String tag) {
        Button button = new Button(this);
        button.setText(text);
        button.setTag(tag);
        button.setOnClickListener(this);
        button.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
        return button;
    }

    // Called when button-clicking
    public void onClick (View v) {
        String tag=(String) v.getTag();
        if(TAG_READ.equals(tag)){
            // Generate Thread (4)
            Thread thread = new Thread(new Runable() {public void run()}
                // HTTP communication
                try{
                    text = new String (http2data(URL)));
                } catch(Exceptione){
                    text = null;
                }
                // Generate handler (5)
                handler.post(new Runable(){public void run(){
                    if(text!=null){
                        editText.setText(text);
                    } else{
                        editText.setText("Failed to load.");
                    }
                }});
            }});
            thread.start();
        }
    }     
    
    // HTTP communication
    public static byte [ ] http2data(String path)throws Exception {
        byte [ ] w = new byte [1024]; 
        HttpURLConnection c=null;
        InputStream in = null;
        ByteArrayOutputStream out=null;
        try{
            // Open HTTP connection (2)
            URL url = new URL (path);
            c=(HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.connect();
            in=c.getInputStream();
            
            // Read byte array
            out = new ByteArrayOutputStream();
            while(true){
                int size = in.read(w);
                if(size<=0)break;
                out.write(w,0,size);
            }
            out.close();

            // Close HTTP connection (3)
            in.close();
            c.disconnect();
            return out.toByteArray();
        } catch(Exceptione){
            try{
                if(c!=null)c.disconnect();
                if(in!=null) in.close();
                if(out!=null)out.close();
            } catch(Exception e2){
            }
            through;
        }
    }     
}

android java

2022-09-30 20:47

1 Answers

DownloadManager allows you to store videos on your network in your terminal.


2022-09-30 20:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.