0

I want to develop a simple code for Android. It run on Emulator and downloaded *.wmv file but it is not run on my Samsung Galaxy S3!!!

Ho can i solve this problem? Anyone know that issue?

This software shoul the followings;

- Video file download from any URL (http://www.MySite.com/test/test.wmv)
- Save to SD Card or to system area/drive

Developing Tools:

Eclipse Kepler,

Min. Req. SDK: API 8: Android 2.2. (Froyo)

Target SDK: API 18

Compile With: API 19: Android 4.4.2

Here ist my codes rows;

AndroidManifest:

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

MainActivity.java

package op.software.videodownloadtest1;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;


public class IlkMainActivity extends Activity {
private final int TIMEOUT_CONNECTION= 5000; // 5 sec.
private final int TIMEOUT_SOCKET=30000; // 30 sec.
private String filePathOP = "...";


public void DownloadFromURL(String videoURL, String fileName){
    FileOutputStream outStream = null;
    BufferedInputStream inStream = null;

    try {
        URL url = new URL(videoURL);

        String storagePath = "/storage/extSdCard/Android/data/op.software.video/";
        File file = new File(storagePath, fileName);
        TextView tView = (TextView) findViewById(R.id.ResultText);
        tView.setText("Download Path: " + file.getPath() + "---" + file.getName());
        filePathOP = file.getPath() + "---" + file.getName();

        long startTime = System.currentTimeMillis();
        Log.i("VideoDownloaderTest1", "Download has started!");
        Log.i("VideoDownloaderTest1", "Download url:" + url);
        Log.i("VideoDownloaderTest1", "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        //this timeout affects how long it takes for the app to realize there's a connection problem
        ucon.setReadTimeout(TIMEOUT_CONNECTION);
        ucon.setConnectTimeout(TIMEOUT_SOCKET);

        //Define InputStreams to read from the URLConnection.
        InputStream is = ucon.getInputStream();
        inStream = new BufferedInputStream(is, 1024 * 5);
        outStream = new FileOutputStream(file);
        byte[] buff = new byte[5 * 1024];

        //Read bytes (and store them) until there is nothing more to read(-1)
        int len;
        while ((len = inStream.read(buff)) != -1)
        {
            outStream.write(buff,0,len);
        }

        Log.i("VideoDownloaderTest1", "download completed in "
                + ((System.currentTimeMillis() - startTime) / 1000)
                + " sec");

        tView = (TextView) findViewById(R.id.ResultText);
        tView.setText("Download başarılı :)" + filePathOP);

        DialogShowMsg("Download succesfull", "File did download \r\n" + filePathOP);

    } catch (IOException e) {
        Log.i("VideoDownloaderTest1", "!!! Error !!!: " + e);
        TextView tView = (TextView) findViewById(R.id.ResultText);
        DialogShowMsg("Download Unsuccessful", "Error \r\n" + e.toString() +"\r\n" + filePathOP);
        tView.setText(e.toString());
    }
    finally {
        try{
            //clean up
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
            if (inStream != null)
                inStream.close();
        } catch (IOException ioe){
            Log.i("VideoDownloaderTest2", "!!! Error !!!: " + ioe);
            TextView tView = (TextView) findViewById(R.id.ResultText);
            tView.setText(ioe.toString());
        }

    }
}

private void DialogShowMsg(String alertTitle, String alertContent){
    AlertDialog.Builder builder = new AlertDialog.Builder(IlkMainActivity.this);

    builder.setMessage(alertContent)
           .setTitle(alertTitle);

    AlertDialog dialog = builder.create();

    dialog.show();
}

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

    Button btn = (Button) findViewById(R.id.HereDownload);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DownloadFromURL("http://www.mysite.com/test/test.wmv", "test.wmv");
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ilk_main, menu);
    return true;
}

}

1 Answers1

1

Use AsyncTask/ExecutorService/Thread to make URl Connection android.os.NetworkOnMainThreadException

  • Hi @Constantin, could you explain? like [link](http://twigstechtips.blogspot.de/2013/08/android-how-to-fix-networkonmainthreade.html) or what else ??? – user3596485 Mar 08 '14 at 13:20
  • http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html -- Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial. – Constantin Cerberus Mar 09 '14 at 07:34
  • Hi Constantin, I try again with asyncTask but i can not do it :( Could you contact me in kuzupalawork@yahoo.com Thanx... – user3596485 Mar 10 '14 at 15:17
  • Hi Constantin, I did not to that. I need your help, please write to me an email kuzupalawork@yahoo.com – user3596485 Mar 11 '14 at 23:12