/*TestService.java */
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.ITestService;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.os.Environment;
public class TestService extends ITestService.Stub {
private static final String TAG = "TestService";
private TestWorkerThread mWorker;
private TestWorkerHandler mHandler;
private Context mContext;
public TestService(Context context) {
super();
mContext = context;
mWorker = new TestWorkerThread("TestServiceWorker");
mWorker.start();
Log.i(TAG, "Spawned worker thread");
}
public void setValue(String url) {
Log.i(TAG, "set URL " + url);
Message msg = Message.obtain();
msg.what = TestWorkerHandler.MESSAGE_SET;
msg.obj = url;
mHandler.sendMessage(msg);
}
private class TestWorkerThread extends Thread {
public TestWorkerThread(String name) {
super(name);
}
public void run() {
Looper.prepare();
mHandler = new TestWorkerHandler();
//mHandler.download();
Looper.loop();
}
}
private class TestWorkerHandler extends Handler {
private static final int MESSAGE_SET = 0;
//String recvUrl ;
@Override
public void handleMessage(Message msg) {
try {
if (msg.what == MESSAGE_SET) {
//recvUrl = msg.obj.toString();
Log.i(TAG, "url received: " + msg.obj.toString());
String fileName ;
String receivedUrl = msg.obj.toString();
for (int i = 1; i <= 10; i++) {
receivedUrl = msg.obj.toString();
fileName = "v" + String.valueOf(i) + ".mp4";
receivedUrl += fileName;
Log.d(TAG,receivedUrl);
try {
URL url = new URL(receivedUrl);
InputStream input = url.openStream();
Log.d(TAG, input.toString());
/***** ERROR OCCURS HERE ****/
File storagePath = new File(Environment.getExternalStorageDirectory() + "/Movies");
Log.d(TAG," Storage path-"+ storagePath+" filename- "+fileName);
/***** ERROR OCCURS HERE ****/
OutputStream output = new FileOutputStream(new File(storagePath, fileName));
Log.d(TAG, output.toString());
try {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
input.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//Log.i(TAG, "url received: " + recvUrl);
}
}catch (Exception e) {
// Log, don't crash!
Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
}
}
}
}
Maybe you should use adb logcat *:e to get full stack and details of the error.
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.
Did you check the return value of Environment.getExternalStorageDirectory()? I've seen issues with this method before, when it was used from the system context.
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platform+unsubscribe@googlegroups.com.
To post to this group, send email to android-platform@googlegroups.com.