選択したビデオを SD カードから ASP.NET サーバーにアップロードするプログラムを作成しました。アップロード中のファイルのステータスを表示する進行状況バーを追加したいですか? 誰でもこの問題について私を助けることができますか? 私は少し混乱しています。ステータスを表示するためにさまざまな方法を試しましたが、成功しませんでした。
助けてくれてありがとう。
package com.isoft.uploder;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class VideoUploader extends Activity
{
/** Called when the activity is first created. */
public static final int SELECT_VIDEO=1;
public static final String TAG="UploadActivity";
String path="";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button camera =(Button)findViewById(R.id.camera);
Button back= (Button)findViewById(R.id.back1);
Button select=(Button)findViewById(R.id.select);
//Video Çek
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
});
//
//Video Seç
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openGaleryVideo();
}
});
//
//Geri Dön
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
//
}
//Gallery'i aç
public void openGaleryVideo()
{
Intent intent=new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"),SELECT_VIDEO);
}
//Dosyayı seç ve yükle
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
Uri videoUri = data.getData();
path= getPath(videoUri);
doFileUpload();
}
}
}
//SD carddan yerini al
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Video.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
//upload et
public void doFileUpload()
{
File file=new File(path);
String urlServer = "http://192.168.10.177/androidweb/default.aspx";
String filename=file.getName();
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 10*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlServer);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setFixedLengthStreamingMode((int) file.length());
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("SD-FileName", filename);//This will be the file name
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}//end of while statement
//Tekrar video seçmek için
setContentView(R.layout.end);
//event
Button back2=(Button)findViewById(R.id.back2);
Button select2=(Button)findViewById(R.id.new1);
back2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
select2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openGaleryVideo();
}
});
//
//int serverResponseCode = connection.getResponseCode();
//String serverResponseMessage = connection.getResponseMessage();
//Log.d("ServerCode",""+serverResponseCode);
//Log.d("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}//end of try body
catch (Exception ex)
{
//ex.printStackTrace();
Log.e("Error: ", ex.getMessage());
}
}
}