Hi ! I am developing a web service. It fetches data from a server. The data in this case is a video file.Now i want this video file to be accessible to others. What should i do?
I have the idea to approach as shown below : (This was done for accessing an image file kept on the server)
class NetworkTask extends AsyncTask { @Override protected void onPreExecute(){ // Display an indeterminate Progress-Dialog myProgressDialog = ProgressDialog.show(SecondActivity.this, "Please wait...", "Trying to get Data", true); } @Override protected String doInBackground(String... path) {
try {
bitmapDrawable= new ServerHandler().getData();
} catch (IOException e) {
e.printStackTrace();
return "error";
}
return "completed";
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
myProgressDialog.dismiss();
if(result!="error"){
if(bitmapDrawable!=null){
displayImage();
}
}else{
showError();
}
}
}
private void showError(){
Toast.makeText(this, "Error in connection", Toast.LENGTH_SHORT).show();
}
private void displayImage(){
ImageView image=(ImageView)findViewById(R.id.image);
image.setBackgroundDrawable(bitmapDrawable);
}
public void onDestroy() {
super.onDestroy();
}
public class ServerHandler { String url="http://172.29.26.40:8080/ExampleService/demo.jpg"; Bitmap bm; BitmapDrawable bitmapDrawable; BufferedOutputStream bufferedOutput = null;
public BitmapDrawable getData() throws IOException{
try {
URL aURL = new URL(url);
URLConnection con = aURL.openConnection();
con.connect();
InputStream is = con.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
bm = BitmapFactory.decodeStream(bis);
bitmapDrawable = new BitmapDrawable(bm);
// routeImage.setImageBitmap(bm);
//routeImage.setBackgroundDrawable(bitmapDrawable);
bis.close();
is.close();
} catch (IOException e) {
Log.e("DEBUG_TAG", "Remtoe Image Exception", e);
throw e;
}
return bitmapDrawable;
}
public void writeToFile() throws IOException{
File file =new File("sdcard/myImage.jpg");
file.createNewFile();
URL u = new URL(url);
URLConnection uc = u.openConnection();
uc.connect();
InputStream in = uc.getInputStream();
FileOutputStream out;
out = new FileOutputStream(file);
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
}
}
public class DataService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
try {
new ServerHandler().writeToFile();
} catch (IOException e) {
Toast.makeText(this, "Error in connection", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
// init the service here
// _startService();
}
}
Will this work for a video file , in a web service ?? Kindly suggest the ...