I have some problem with AsyncTask in my application.
There is AsyncTask which takes File from sd card and makes operations with it.
It is actually giving me a proper result, but while doing the task there are a black screen and a blocked user interface about 3 seconds that obviously would make a user annoyed. I'm wondering how to get rid of this problem.
So here is the code:
ParseXMLTask.java:
public ParseXMLTask(Context context, IPostParse iPostParse, ProgressBar progressBar) {
this.context = context;
this.iPostParse = iPostParse;
this.progressBar = progressBar;
}
#Override
protected Intent doInBackground(File... params) {
File file = params[0];
Intent pack = new Intent(context, PackActivity.class);
/* some heavy parsing task */
return pack;
}
#Override
protected void onPreExecute() {
progressBar.setIndeterminate(true);
}
#Override
protected void onPostExecute(Intent result) {
iPostParse.postParse(result);
}
LoadingActivity.java:
The executing task code:
private void parseXML() {
File file = new File(PATH + fileName + ".xml");
ParseXMLTask parseTask = new ParseXMLTask(this, this, loadingBar);
parseTask.execute(file);
}
And the implemented method of the interface, nothing special:
public void postParse(Intent result) {
result.putExtra("name", packName);
result.putExtra("author", packAuthor);
result.putExtra("date", packDate);
result.putExtra("file", fileName);
result.putExtra("votes", votes);
startActivity(result);
}
Hope you help me to solve this problem, thanks!
This can happen if you are putting too much data into your Intent's extras.
Related
I have implemented the file upload to Amazon S3 following the Amazon guide and I have noticed that it is too slow. It takes around 10 sec to upload a simple png file around 20kb.
Initially I thought that the problem was related with threads and I have implemented an AsyncTask to upload the image, but the problem is still there. Following is the code employed to upload the image.
private class UploadFileTask extends AsyncTask<String, Integer, String> {
String remotePath;
String remoteFileName;
File file;
Context context;
S3UploadInterface listener;
public UploadFileTask(Context context,String remotePath,String remoteFileName, File file, S3UploadInterface listener){
this.context=context;
this.remotePath=remotePath;
this.remoteFileName=remoteFileName;
this.file=file;
this.listener=listener;
}
protected String doInBackground(String... params) {
credentialsProvider = new CognitoCachingCredentialsProvider(context,
"MY_PRIVATE_CREDENTIAL",
Regions.US_EAST_1);
TransferManager transferManager = new TransferManager(credentialsProvider);
Upload upload = transferManager.upload(remotePath, remoteFileName, file);
TransferProgress transferred = upload.getProgress();
while (!upload.isDone()) {
try {
publishProgress((int) transferred.getPercentTransferred());
} catch (Exception e) {
listener.uploadFailed(e);
}
}
return "uploaded";
}
protected void onProgressUpdate(Integer... progress) {
if (listener!=null)
listener.currentUploadProgress(progress[0]);
}
protected void onPostExecute(String result) {
if (listener!=null)
listener.uploadCompleted();
}
}
Any idea to solve this problem?
Thanks :)
This is going to hammer your CPU
while (!upload.isDone()) {
try {
publishProgress((int) transferred.getPercentTransferred());
} catch (Exception e) {
listener.uploadFailed(e);
}
}
Try adding Thread.sleep to give other threads time to run.
Actually It is changing but into Blank!
In my App, i going to get my Images-Path from Database and show them in main activity. this process achieves by selecting thumbnails of images in DialogFragment and sending their Record ID to main Activity withing an Interface for Communicating between Fragment-Activity.
at this point i am going to use an Asynctask inner class to get FilePath of selected Image and Show it to ImageView.
everythings works perfectly EXCEPT that the ImageView does not show it properly and it just show blank!
P.S.: the FilePath is Correct, i used it in onActivityResult on some place in app.
Thank you.
Main Activity code:
public class AnnotateDiagramActivity extends Activity implements Communicator {
#Override
public void respond(String data , int requestCode) {
//Passing Project ID from CPDialogFragment or PLDialogFragment
switch(requestCode) {
case OPEN_PROJECTID:
PROJECTID = Integer.valueOf(data);
loadProject(PROJECTID);
ToastShort(data);
break;
default:
break;
}
}
public class loadMainImage extends AsyncTask<Project,Integer,String>
{
#Override
protected String doInBackground(Project... params) {
return project.GetFilePath();
}
protected void onPostExecute( String result) {
//here is my main issue:
fragmentView.setImageBitmap(new ImageDecoder().decodeFile(new File(project.GetFilePath())));
fragmentView.postInvalidate();
}
}
public void loadProject(int ID)
{
project = new Project();
project= expertDbHelper.getProject(ID);
SharedPreferences openedProject = getSharedPreferences("openedProject", 0);
SharedPreferences.Editor projectEditor = openedProject.edit();
projectEditor.putInt("id",project.GetID());
projectEditor.putString("filePath",project.GetFilePath());
projectEditor.commit();
new loadMainImage().execute(project);
}
}
EDIT For Verifying on Returning Image From File Path:
protected void onPostExecute( String result) {
ImageDecoder imageDecoder = new ImageDecoder();
Bitmap bmp = null;
File file =new File(result);
if(file.isFile()) {
bmp=imageDecoder.decodeFile(file,500);
fragmentView.setImageBitmap(bmp);
fragmentView.postInvalidate();
}
else
{ Log.e("Async Task Is File: " ,String.valueOf(file.isFile()));}
}
I've found that my Image does not set properly due to some criteria that calculated from previous loaded image, so i check my code and find (layout boolean parameter) in my code that needs to be reset.
protected void onPostExecute( String result) {
ImageDecoder imageDecoder = new ImageDecoder();
Bitmap bmp = null;
File file =new File(result);
Log.e("Async Task Result: " ,result);
if(file.isFile()) {
bmp=imageDecoder.decodeFile(file,500);
fragmentView.reset();
fragmentView.recycle();
fragmentView.layout= false;
fragmentView.setImageBitmap(bmp);
}
else
{
Log.e("Async Task Is File: " ,String.valueOf(file.isFile()));
}
}
I have an Activity, which starts an AsyncTask with an Implemented process dialog. That works fine! But i want to get a String return when the asyncTask has finished. So i have to return something in the onPostExecute - Method.
That result(string) i want to grab in the Activity, which started the AsyncTask.
I do not want to use .get() because it blocks the UI thread.
What do i have to write into onPostExecute and the Activity grab the string from doInBackground?
Thank you for any kind of help to solve this problem ;)
Now with Code:
class BgDL extends AsyncTask<String, Integer, String> {
String finishString="";
private Context context;
ProgressDialog pdialog;
public BgDL(Context cxt) { //get the context (usually "this" from Activity / otherwise progressdialog wont show up!
context = cxt;
pdialog = new ProgressDialog(context);
}
#Override
protected String doInBackground(String... strings) {
OutputStream output;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(strings[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
if (strings[1]=="toString") { // write byte to string if a file is not given
baos= new ByteArrayOutputStream();
output = new DataOutputStream(baos);
} else { //otherwise to string
output = new FileOutputStream(strings[1]);
}
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
if (strings[1]=="toString") {
finishString = baos.toString(); //
} // only write byte to string if a file is not given
} catch (Exception e) {log.d("ex",e.toString());
}
return finishString;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pdialog.setTitle("Please wait");
pdialog.setIndeterminate(false);
pdialog.setMax(100);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdialog.show();
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
pdialog.setProgress(progress[0]);
}
protected void onPostExecute(String...finishString) {
pdialog.dismiss();//!!!!!!!!!finishString i want to pass over to my Activity, which started this asynctask with .execute();
}
Create a class in your project which extends activity as shown below:
public class SomeClass extends Activity
{
public void dataFromPostExecute(String data)
{
System.out.println("in mainactivity");
}
}
If you want a single thread for every activity, just create a class which extends
Application
public class Async extends Application
{
private Socket globalSocket;
#Override
public void onCreate()
{
//socket = null;
}
public Socket getglobalSocket()
{
return globalSocket;
}
public void setGlobalSocket(Socket globalSocket)
{
this.globalSocket = globalSocket;
}
}
In your socket class which extends Asynctask do the following:
public SocketClass extends AsyncTask<String,String,String>
{
Async app;
private SomeClass t_act;
public SocketClass(SomeClass sct)
{
t_act = sct;
this.con = tst;
app= ((Async)sct.getApplicationContext());
}
protected void onPostExecute(String data)
{
t_act.dataFromPostExecute(data);
}
}
Then, in your activity extend SomeClass and do as shown below:
public class Activity1 extends SomeClass
{
public void dataFromPostExecute(String data)
{
//do whatever you want. "data" of this method contains the values from
postexecute()
}
}
Your return value from doInBackground() is you formal in onPostExecute().
So you should just be able to pass it in.
What do i have to write into onPostExecute and the Activity grab the
string from doInBackground?
When you are using AsyncTask then you can update your UI only on onProgressUpdate and onPostExecute method.
Your doInBackground() method returns some data and these data is going to onPostExecute method(it depends also how your generic are declared).
Generally, there is no another approaches how to do it.
You meant this:
AsyncTask a = new AsyncTask(Context);
a.execute(Input);
First means that your constructor looks like
public MyAsync(Context c) {
this.c = c;
}
Second means that you declared your first generic type(assumption Input param is String) as
private class MyAsync extends AsyncTask<String, Void, String> {
//...
}
And you want to update UI with String that returns doInBackground() method and merely place is onPostExecute method with IN parameter String that returns doInBackground().
protected void onPostExecute(String stringReturnedFromDoInBackground)
{
// some work
}
So if you want to do it in different way, change your application logic and use for example ResultReceiver with IntentService.
Am develop the Frame application. For that i want to display images(frames) from url. the url has more than 50 images. For that i use gridview but it lacks some points such as,
1.It speeds very slow to load images.
2.We declare the name and size of the images at code time so that we dont add images to the url after publishing the applicaton.
I need solution for these asap. Please anyone give me suggestion.
Use below link of lazy loading listview, this will help you.
Lazy Loading ListView
Use Above link Code and Add another activity and another layout for display selected image, if u have any issue than tell me, i will put full code here.
1.It speeds very slow to load images.
It will depend on You bandwidth and device Cache.
2. We declare the name and size of the images at code time so that we dont add images to the url after publishing the application.
You can have predefine URL so at code time you can append the image name to url.and once you have URL ready using AsyncTask download images one by one \
The below snippets will help you.
DownloadHelper.java
public interface DownloadHelper
{
public void OnSucess(Bitmap bitmap);
public void OnFailure(String response);
}
MainActivity.java
public class GalleryExample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
DownloadHelper downloadHelper = new DownloadHelper()
{
#Override
public void OnSucess(Bitmap bitmap)
{
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
}
#Override
public void OnFailure(String response)
{
Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
};
new MyTask(this,downloadHelper).execute("image url");
}
MyTask.java
public class DownloadTask extends AsyncTask<String, Integer, Object>
{
private Context context;
private DownloadHelper downloadHelper;
private ProgressDialog dialog;
public DownloadTask(Context context,DownloadHelper downloadHelper)
{
this.context = context;
}
#Override
protected void onPreExecute()
{
dialog = new ProgressDialog(context);
dialog.setTitle("Please Wait");
dialog.setMessage("Fetching Data!!");
dialog.setCancelable(false);
dialog.show();
super.onPreExecute();
}
#Override
protected Object doInBackground(String... params)
{
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
}
#Override
protected void onPostExecute(Object result)
{
dialog.dismiss();
if (result != null)
{
downloadHelper.OnSucess((Bitmap)result);
}
else
{
downloadHelper.OnFailure("Error in Downloading Data!!");
}
super.onPostExecute(result);
}
}
I've developed an application that takes content from the internet and shows it accordingly on the device's screen . The program works just fine , a little bit slow . It takes about 3-4 seconds to load and display the content . I would like to put my code that does all the work ( grabbing web content and displaying it) in a background thread . Also , I'd like to show a progress dialog .
public class Activity1 extends Activity
{
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute()
{
/*
* This is executed on UI thread before doInBackground(). It is
* the perfect place to show the progress dialog.
*/
progressDialog = ProgressDialog.show(Activity1.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
/*
* This is run on a background thread, so we can sleep here
* or do whatever we want without blocking UI thread. A more
* advanced use would download chunks of fixed size and call
* publishProgress();
*/
Thread.sleep(params[0]);
// HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
/*
* The task succeeded
*/
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
/*
* Update here your view objects with content from download. It
* is save to dismiss dialogs, update views, etc., since we are
* working on UI thread.
*/
AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
b.setTitle(android.R.string.dialog_alert_title);
if (result)
{
b.setMessage("Download succeeded");
}
else
{
b.setMessage("Download failed");
}
b.setPositiveButton(getString(android.R.string.ok),
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dlg, int arg1)
{
dlg.dismiss();
}
});
b.create().show();
}
}.execute(2000);
/* new Thread()
{
#Override
public void run()
{
// dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
}*/
}
If I run the application with this code , I get this : download failed . On the other hand , if I keep the final thread , the app crashes , NullPointerException . I really don't know what to do anymore .
I would really appreaciate if you could give me an alternative to this code , not just some hints because I'm new to android and I really don't know much . Thanks.
UPDATE :
I don't want to display the progress of the download , I want to display the progress dialog until the app is ready to display the full content.
The best approach to do this is by using the AsyncTask class, as it will allow you to execute some background process and update the UI at the same time (in your case, it's a progress bar).
This is an example code:
ProgressDialog mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("the url to the file you want to download");
The AsyncTask will look like this:
private class DownloadFile extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
The method above (doInBackground) runs always on a background thread. You shouldn't do any UI tasks there. On the other hand, the onProgressUpdate runs on the UI thread, so there you will change the progress bar:
#Override
public void onProgressUpdate(String... args){
// here you will have to update the progressbar
// with something like
mProgressDialog.setProgress(args[0]);
}
}
You will also want to override the onPostExecute method if you want to execute some code once the file has been downloaded completely.
You should create an inner class for AsyncTask like this :
private class YourTask extends AsyncTask<Context, Void, Void>
{
ProgressDialog dialog = new ProgressDialog(mContext);
protected void onPreExecute()
{
dialog.setMessage("loading..");
dialog.show();
}
protected Void doInBackground(Context... params)
{
// ...
return null;
}
protected void onPostExecute(final Void unused)
{
dialog.dismiss();
}
}
and in onCreate() put :
new YourTask().execute();
and for more detail you should check this once:
http://developer.android.com/reference/android/os/AsyncTask.html
When you use the new thread, your app crashes because the progress dialog is not initialized there
Inside your new thread use:
`progressDialog = ProgressDialog.show(Activity1.this, "","Loading...");
and about that alert dialog: Basically either params is null or the logic is throwing some exception. It's not returning true
so check the ddms logs and post them here.
`