I have a service running that uploads photos. I have a progressbar that is updated by NotificationManager, however its being called multipletimes.
IntentService[n identical 127 lines
How can I make it so that the manager only notifies when increments of 10% are uploaded?
#Override
public void writeTo(#NonNull BufferedSink sink) throws IOException {
long fileLength = mFile.length();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
FileInputStream in = new FileInputStream(mFile);
long uploaded = 0;
int read;
while (is_uploading && (read = in.read(buffer)) != -1) {
int percent = 10* (int) (10 * uploaded / fileLength);
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent + 10));
uploaded += read;
sink.write(buffer, 0, read);
}
}
Edit:
#Override
public void writeTo(#NonNull BufferedSink sink) throws IOException {
long fileLength = mFile.length();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
FileInputStream in = new FileInputStream(mFile);
long uploaded = 0;
int read;
while (is_uploading && (read = in.read(buffer)) != -1) {
int percent = 10* (int) (10 * uploaded / fileLength);
if(percent % 10 == 0){
Log.d(TAG, "writeTo: test");
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent + 10));
}
uploaded += read;
sink.write(buffer, 0, read);
}
}
The Log.d(TAG, "writeTo: test"); is still being called many times even though it should only be 10 times.
IntentService[n identical 346 lines
Use below code:
int percent = (int) (100 * uploaded / fileLength);
if(percent % 10 == 0 && lastPercent != percent){
lastPercent = percent;
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent));
}
also create globle variable public int lastPercent = 0;
I am implementing a download manager in native android where a thread pool executor is used to implement parallel downloads. A runnable is where the actual download happens, which is being executed on the pool threads. How can I send the download progress from the runnable to the UI? In order to send broadcasts, I need to pass context into the runnable. Is that the appropriate way?
How can I handle pause/resume/cancel of download gracefully?
Right now the moment user taps the pause/cancel button the value is updated in the DB and while the Thread.CurrentThread().IsInterrupted condition in the runnable becomes valid I check the status in database and decide whether I need to delete the partially downloaded file (if its cancel).
Also, will it be possible to know when the download completes so that I can remove the future object from the list?
public class Downloadable : Java.Lang.Object, IRunnable
{
private readonly string _destination;
private readonly int _productId;
public Downloadable(int productId)
{
_productId = productId;
_destination = Utils.StoragePath() + productId + ".zip";
}
public void Run()
{
int count;
try
{
Response response = CloudService.GetCloud().GetDownLoadURL(_productId.ToString(), true).Result;
if (string.Equals(response.status, "error", StringComparison.OrdinalIgnoreCase) || string.Equals(response.status, "internalError", StringComparison.OrdinalIgnoreCase))
{
//send error
}
else
{
DownloadPath downloadPath = JsonConvert.DeserializeObject<DownloadPath>(response.data);
string offlineUrl = downloadPath.contentUrl.Offline;
if (string.IsNullOrWhiteSpace(offlineUrl))
{
//send error
}
else
{
File directory = new File(Utils.StoragePath());
if (!directory.Exists())
directory.Mkdirs();
URL url = new URL(offlineUrl);
HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();
long total = 0;
File file = new File(_destination);
file.CreateNewFile();
if (file.Exists() && file.Length() > 0)
{
total = file.Length();
connection.SetRequestProperty("Range", "Bytes=" + total + "-");
}
connection.Connect();
int lenghtOfFile = connection.ContentLength;
BufferedInputStream bufferedInputStream = new BufferedInputStream(url.OpenStream());
FileOutputStream fileOutputStream = new FileOutputStream(_destination, true);
byte[] buffer = new byte[1024];
count = 0;
while ((count = bufferedInputStream.Read(buffer, 0, 1024)) != -1)
{
if (Thread.CurrentThread().IsInterrupted)
{
if (DBService.GetDB().GetStatus(_productId) == (int)IpcCommon.Enumerations.Status.DOWNLOAD)
file.Delete();
break;
}
total += count;
System.Console.WriteLine("__PROGRESS__ " + (int)((total * 100) / lenghtOfFile));
System.Console.WriteLine("__PROGRESS__ ID " + _productId);
//publishProgress("" + (int)((total * 100) / lenghtOfFile));
fileOutputStream.Write(buffer, 0, count);
}
fileOutputStream.Close();
bufferedInputStream.Close();
}
}
}
catch (System.Exception exception)
{
IpcCommon.App.Logger.Log("Downloadable - File Download", new System.Collections.Generic.Dictionary<string, string> { { "Error", exception.Message } });
}
}
}
Dictionary<int, IFuture> _runningTaskList = new Dictionary<int, IFuture>();
int noOfCores = Runtime.GetRuntime().AvailableProcessors();
LinkedBlockingQueue _taskQueue = new LinkedBlockingQueue();
_threadPoolExecutor = new ThreadPoolExecutor(noOfCores, noOfCores * 2, 1, TimeUnit.Minutes, _taskQueue);
IFuture future = _threadPoolExecutor.Submit(new Downloadable(productId));
_runningTaskList.Add(productId, future);
I'm using this code to download from a URL ,it works great with android 4,but in the other hand it doesn't work with android 2.3. Can someone tell what have i done wrong ?
URL url = new URL(sUrl);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(pathFolder+"/"+fileName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
It works for me. Here is my method:
private boolean dowloadFile(String url, File saveFile) {
int BUFF_SIZE = 1024 * 1024; //1Mo
long length = 0;
long current = 0;
if(saveFile.exists())
current = saveFile.length();
try {
DefaultHttpClient http = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
if(current>0)
request.addHeader("Range", "bytes=" + current + "-");
HttpResponse response = http.execute(request);
if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 206) {
return false;
}
Header[] headers = response.getHeaders("Content-Range");
if(headers.length>0) {
String s = headers[0].getValue();
length = Integer.valueOf(s.subSequence(s.indexOf("/")+1, s.length()).toString());
} else {
Header[] headers2 = response.getHeaders("Content-Length");
if(headers2.length>0)
length = Integer.valueOf(headers2[0].getValue());
if(current>0) {
saveFile.delete();
current = 0;
}
}
BufferedInputStream ls = new BufferedInputStream(response.getEntity().getContent());
long nexttime = 0;
RandomAccessFile fos = new RandomAccessFile(saveFile, "rw");
fos.seek(current);
byte[] buffer = new byte[BUFF_SIZE];
while (current < length) {
boolean buffFull = false;
int currentBuff = 0;
int readSize = 0;
while (buffFull == false) {
readSize = ls.read(buffer, currentBuff, BUFF_SIZE - currentBuff);
if (readSize == -1)
buffFull = true;
else {
currentBuff += readSize;
if (currentBuff == BUFF_SIZE)
buffFull = true;
}
}
fos.write(buffer, 0, currentBuff);
current += currentBuff;
long time = SystemClock.elapsedRealtime();
if (nexttime < time) {
// Progress
nexttime = time + 1000;
}
}
fos.close();
// Progress Finish
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}
I hope I have helped you !
I have made a voice recorder app, and I want to show the duration of the recordings in a listview. I save the recordings like this:
MediaRecorder recorder = new MediaRecorder();
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
folder = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Audio recordings");
String[] files = folder.list();
int number = files.length + 1;
String filename = "AudioSample" + number + ".mp3";
File output = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Audio recordings" + File.separator
+ filename);
FileOutputStream writer = new FileOutputStream(output);
FileDescriptor fd = writer.getFD();
recorder.setOutputFile(fd);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
e.printStackTrace();
}
How can I get the duration in seconds of this file?
Thanks in advance
---EDIT
I got it working, I called MediaPlayer.getduration() inside the MediaPlayer.setOnPreparedListener() method so it returned 0.
MediaMetadataRetriever is a lightweight and efficient way to do this. MediaPlayer is too heavy and could arise performance issue in high performance environment like scrolling, paging, listing, etc.
Furthermore, Error (100,0) could happen on MediaPlayer since it's a heavy and sometimes restart needs to be done again and again.
Uri uri = Uri.parse(pathStr);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(AppContext.getAppContext(),uri);
String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int millSecond = Integer.parseInt(durationStr);
The quickest way to do is via MediaMetadataRetriever. However, there is a catch
if you use URI and context to set data source you might encounter bug
https://code.google.com/p/android/issues/detail?id=35794
Solution is use absolute path of file to retrieve metadata of media file.
Below is the code snippet to do so
private static String getDuration(File file) {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(file.getAbsolutePath());
String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
return Utils.formateMilliSeccond(Long.parseLong(durationStr));
}
Now you can convert millisecond to human readable format using either of below formats
/**
* Function to convert milliseconds time to
* Timer Format
* Hours:Minutes:Seconds
*/
public static String formateMilliSeccond(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
// return String.format("%02d Min, %02d Sec",
// TimeUnit.MILLISECONDS.toMinutes(milliseconds),
// TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
// TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
// return timer string
return finalTimerString;
}
Either try this to get duration in milliseconds:
MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(pathofyourrecording));
int duration = mp.getDuration();
Or measure the time elapsed from recorder.start() till recorder.stop() in nanoseconds:
long startTime = System.nanoTime();
// ... do recording ...
long estimatedTime = System.nanoTime() - startTime;
Try use
long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds
long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds
Division on 1000 to convert to seconds.
Hope this helped you.
If the audio is from url, just wait for on prepared:
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
length = mp.getDuration();
}
});
Kotlin Extension Solution
You can add this to reliably and safely get your audio file's duration. If it doesn't exist or there is an error, you'll get back 0.
myAudioFile.getMediaDuration(context)
/**
* If file is a Video or Audio file, return the duration of the content in ms
*/
fun File.getMediaDuration(context: Context): Long {
if (!exists()) return 0
val retriever = MediaMetadataRetriever()
return try {
retriever.setDataSource(context, uri)
val duration = retriever.extractMetadata(METADATA_KEY_DURATION)
retriever.release()
duration.toLongOrNull() ?: 0
} catch (exception: Exception) {
0
}
}
If you are regularly working with String or Uri for files, I'd suggest also adding these useful helpers
fun Uri.asFile(): File = File(toString())
fun String?.asUri(): Uri? {
try {
return Uri.parse(this)
} catch (e: Exception) {
Sentry.captureException(e)
}
return null
}
fun String.asFile() = File(this)
According to Vijay's answer, The function gives us the duration of the audio/video file but unfortunately, there is an issue of a run time exception so I sorted out and below function work properly and return the exact duration of the audio or video file.
public String getAudioFileLength(String path, boolean stringFormat) {
StringBuilder stringBuilder = new StringBuilder();
try {
Uri uri = Uri.parse(path);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(HomeActivity.this, uri);
String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int millSecond = Integer.parseInt(duration);
if (millSecond < 0) return String.valueOf(0); // if some error then we say duration is zero
if (!stringFormat) return String.valueOf(millSecond);
int hours, minutes, seconds = millSecond / 1000;
hours = (seconds / 3600);
minutes = (seconds / 60) % 60;
seconds = seconds % 60;
if (hours > 0 && hours < 10) stringBuilder.append("0").append(hours).append(":");
else if (hours > 0) stringBuilder.append(hours).append(":");
if (minutes < 10) stringBuilder.append("0").append(minutes).append(":");
else stringBuilder.append(minutes).append(":");
if (seconds < 10) stringBuilder.append("0").append(seconds);
else stringBuilder.append(seconds);
}catch (Exception e){
e.printStackTrace();
}
return stringBuilder.toString();
}
:)
You can use this readyMade method, hope this helps someone.
Example 1 : getAudioFileLength(address, true); // if you want in stringFormat
Example 2 : getAudioFileLength(address, false); // if you want in milliseconds
public String getAudioFileLength(String path, boolean stringFormat) {
Uri uri = Uri.parse(path);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(Filter_Journals.this, uri);
String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int millSecond = Integer.parseInt(duration);
if (millSecond < 0) return String.valueOf(0); // if some error then we say duration is zero
if (!stringFormat) return String.valueOf(millSecond);
int hours, minutes, seconds = millSecond / 1000;
hours = (seconds / 3600);
minutes = (seconds / 60) % 60;
seconds = seconds % 60;
StringBuilder stringBuilder = new StringBuilder();
if (hours > 0 && hours < 10) stringBuilder.append("0").append(hours).append(":");
else if (hours > 0) stringBuilder.append(hours).append(":");
if (minutes < 10) stringBuilder.append("0").append(minutes).append(":");
else stringBuilder.append(minutes).append(":");
if (seconds < 10) stringBuilder.append("0").append(seconds);
else stringBuilder.append(seconds);
return stringBuilder.toString();
}
For me, the AudioGraph class came to the rescue:
public static async Task<double> AudioFileDuration(StorageFile file)
{
var result = await AudioGraph.CreateAsync(new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Speech));
if (result.Status == AudioGraphCreationStatus.Success)
{
AudioGraph audioGraph = result.Graph;
var fileInputNodeResult = await audioGraph.CreateFileInputNodeAsync(file);
return fileInputNodeResult.FileInputNode.Duration.TotalSeconds;
}
return -1;
}
Kotlin shortest way to do it (if it is an audiofile):
private fun getDuration(absolutePath: String): String {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(absolutePath)
//For format in string MM:SS
val rawDuration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?: 0L
val duration = rawDuration.milliseconds
return format("%02d:%02d", duration.inWholeMinutes, duration.inWholeSeconds % 60)
}
private fun getDurationInSeconds(absolutePath: String): Long {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(absolutePath)
//Return only value in seconds
val rawDuration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?: 0L
return rawDuration.milliseconds.inWholeSeconds
}
After you write the file, open it up in a MediaPlayer, and call getDuration on it.
Have you looked at Ringdroid?. It's pretty light weight and the integration is straight forward. It works well with VBR media files as well.
For your problem with getting the duration, you might want to do something like below using Ringdroid.
public class AudioUtils
{
public static long getDuration(CheapSoundFile cheapSoundFile)
{
if( cheapSoundFile == null)
return -1;
int sampleRate = cheapSoundFile.getSampleRate();
int samplesPerFrame = cheapSoundFile.getSamplesPerFrame();
int frames = cheapSoundFile.getNumFrames();
cheapSoundFile = null;
return 1000 * ( frames * samplesPerFrame) / sampleRate;
}
public static long getDuration(String mediaPath)
{
if( mediaPath != null && mediaPath.length() > 0)
try
{
return getDuration(CheapSoundFile.create(mediaPath, null));
}catch (FileNotFoundException e){}
catch (IOException e){}
return -1;
}
}
Hope that helps
It's simply. use RandomAccessFile Below is the code snippet to do so
public static int getAudioInfo(File file) {
try {
byte header[] = new byte[12];
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
randomAccessFile.readFully(header, 0, 8);
randomAccessFile.close();
return (int) file.length() /1000;
} catch (Exception e) {
return 0;
}
}
You can, of course, be more complete depending on your needs
In my android project i need to display seekbar and network speed(With how much speed the file is Uploading/Downloading from server using FTP Protocol). In my situation when i try to upload a file from emulator sdcard then it's showing the speed and seekbar at the end when the file is stored in the server, but my requirement is i need to show sheekbar and network speed continuously while the file is uploading into server. I am not understand where i did wrong in my code. Below is my code for file upload into ftp server:
Thanks In Advance.
#Override
protected String doInBackground(String... arg0) {
int count = 0;
FTPClient ObjFtpCon = new FTPClient();
//Toast.makeText(con, "FTPasync doInBackground() is called" ,Toast.LENGTH_SHORT).show();
try {
runOnUiThread(new Runnable() {
public void run() {
bar.setProgress(0);
//real_time.setText(0 + " secs");
test_avg.setText(0+ " kbps");
//peak.setText(0+" kbps");
}
});
updateUI(pp, R.drawable.pause);
//ObjFtpCon.connect("ftp.customhdclips.com");
ObjFtpCon.connect("ftp."+map.get("url").toString());
updateUI(status, "Connecting");
//if (ObjFtpCon.login("fstech#customhdclips.com", "fstech123")) {
if (ObjFtpCon.login(map.get("username").toString(), map.get("password").toString())) {
updateUI(status, "Connected");
ObjFtpCon.enterLocalPassiveMode(); // important!
ObjFtpCon.cwd("/");// to send the FTP CWD command to the server, receive the reply, and return the reply code.
//if (mode == 0) {
if(Integer.parseInt((map.get("oprn").toString()))== 0){ // Download File Using FTP Protocol
System.out.println("download test is called");
File objfile = new File(Environment.getExternalStorageDirectory() + File.separator + "/logo.png");
objfile.createNewFile();
FileOutputStream objFos = new FileOutputStream(objfile);
boolean blnresult = ObjFtpCon.retrieveFile("/logo.png", objFos);
objFos.close();
if (blnresult) {
// toast("Download succeeded");
// toast("Stored at : " +
// objfile.getAbsolutePath());
}
//***********************************************************
/*
File objfile = new File(
Environment.getExternalStorageDirectory()
+ File.separator + "/test.txt");
// System.out.println("total" + objfile.getTotalSpace() + " bytes");
objfile.createNewFile();
FileOutputStream objFos = new FileOutputStream(objfile);
boolean blnresult = ObjFtpCon.retrieveFile("/test.txt",
objFos);
objFos.close();
if (blnresult) {
System.out.println("download in ftp is successful");
// toast("Download succeeded");
// toast("Stored at : " +
// objfile.getAbsolutePath());
}*/
}
else {
// Upload
System.out.println("upload test is called");
//Toast.makeText(con, "upload FTP test is called", Toast.LENGTH_SHORT).show();
//ContextWrapper context = null;
//assetManager= context.getAssets();
assetManager = getResources().getAssets();
input1 = assetManager.open("hello.txt");
long total = 0;
long sleepingTime= 0;
final long started = System.currentTimeMillis();
int size = input1.available();
byte[] buffer = new byte[size];
byte dataByte[] = new byte[1024];
//byte dataByte[] = new byte[(int)((CharSequence) input1).length()];
//input1.read(buffer);
//final int lenghtOfFile = data.getBytes().length;
final int lenghtOfFile = input1.toString().getBytes().length;
System.out.println("length of file....."+lenghtOfFile);
ByteArrayInputStream byteArrayIS = new ByteArrayInputStream(input1.toString().getBytes());
System.out.println("ByteArrayIS value is...."+byteArrayIS);
System.out.println("started time --"+started);
updateUI(status, "Uploading");
while ((count = byteArrayIS.read(dataByte)) != -1)
{
System.out.println("read value is...."+byteArrayIS.read(dataByte));
while (sleep1) {
System.out.println("While Loop Sleep1 is Called");
Thread.sleep(1000);
sleepingTime +=1000;
}
total += count;
System.out.println("Total Value IS:: "+total);
System.out.println("Count Value Is:: "+count);
final int progress = (int) ((total * 100) / lenghtOfFile);
System.out.println("Progress Value IS::" + progress);
final long speed = total;
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000; --- When using this line then it's not updating Avarage
boolean result = ObjFtpCon.storeFile("/test.txt", input1);
//boolean result = ObjFtpCon.storeFile(map.get("file_address").toString()+"/test.txt", input1); --- When using this line then it's not updating Status
duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
runOnUiThread(new Runnable() {
public void run() {
//bar.setProgress(progress);
// trans.setText("" + progress);
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
//real_time.setText(duration + " secs");
if (duration != 0) {
test_avg.setText((((speed / duration)*1000)*0.0078125) + " kbps");
bar.setProgress(progress);
/*if (pk <= (speed / duration) / 1024) {
pk = (speed / duration) / 1024;
}*/
if (pk <= ((speed / duration)*1000)*0.0078125) {
pk = (long)(((speed / duration)*1000)*0.0078125);
}
//peak.setText(pk + " kbps");
}
}
});
//in.close();
if (result) {
updateUI(status, "Uploaded");
// toast("Uploading succeeded");
// toast("Uploaded at /test.txt");
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
System.out.println("curreent time..... "+System.currentTimeMillis());
System.out.println("started time --"+started);
System.out.println("sleep tome...."+sleepingTime);
System.out.println("duration is....."+duration);
/*runOnUiThread(new Runnable() {
public void run() {
bar.setProgress(progress);
// trans.setText("" + progress);
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
real_time.setText(duration + " secs");
if (duration != 0) {
test_avg.setText((speed / duration) / 1024
+ " kbps");
if (pk <= (speed / duration) / 1024) {
pk = (speed / duration) / 1024;
}
peak.setText(pk + " kbps");
}
}
});*/
}
/*while(!result){Thread.sleep(1000);}*/
}
byteArrayIS.close();
}
}
else{
System.out.println("password entered is incorrect");
//Toast.makeText(con, "Username or/and password is incorrect", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
e.printStackTrace();
// toast(e.getLocalizedMessage());
}
try {
ObjFtpCon.logout();
ObjFtpCon.disconnect();
}
catch (IOException e) {
e.printStackTrace();
// toast(e.getLocalizedMessage());
}
return null;
}
Since you're calling doInBackground(), I assume you're subclassing AsyncTask. If so, why aren't you using publishProgress and onProgressUpdate? runOnUiThread doesn't execute immediately unless you're already on the UI Thread, and because you're in doInBackground() you're in a background thread, not the UI Thread. There's a possibility that runOnUiThread() isn't actually running until doInBackground() is over. In comparison, publishProgress and onProgressUpdate are specifically designed to report progress from the background thread.