Guest
Title : Untitled
Guest on 20th September 2024 06:29:51 AM
Syntax: MARKDOWN | Views: 6 | Your IP: 18.221.210.95

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;

// Inside MainActivity.java

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

Button downloadButton = findViewById(R.id.downloadButton);
downloadButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Move download task to background thread
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                downloadFile("http://212.183.159.230/50MB.zip");
            }
        });
    }
});

}

private void downloadFile(String urlString) { try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect();

    int responseCode = connection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        String fileName = getFileNameFromURL(urlString);
        File directory = getExternalFilesDir(null);

        if (directory != null) {
            File file = new File(directory, fileName);

            // Log the file path
            Log.d(TAG, "Saving file to: " + file.getAbsolutePath());

            InputStream inputStream = connection.getInputStream();
            OutputStream outputStream = new FileOutputStream(file);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.flush();
            outputStream.close();
            inputStream.close();

            // Update UI on the main thread after successful download
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "File downloaded: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                }
            });
        } else {
            Log.e(TAG, "Failed to get external files directory");
        }
    } else {
        Log.e(TAG, "Server returned HTTP error code: " + responseCode);
    }

} catch (Exception e) {
    Log.e(TAG, "Error downloading file: ", e);
    // Update UI on the main thread in case of failure
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "Failed to download file: " + e.getClass().getSimpleName(), Toast.LENGTH_SHORT).show();
        }
    });
}

}


Edit Paste Earn Money
Raw Paste

What is JustPasteIT.ORG?

JustPasteIT.ORG is a secure and ad-free online platform for sharing code snippets, text, and notes. It allows users to create and share content anonymously by default, using a simple text editor with formatting features. The platform also offers short URLs for easy sharing and the option to save content as text files. It's designed for developers, students, and anyone who needs a quick and private way to share text online without distractions.