2

I want to upload images on a Parse server using an AsnycTask. When I upload images without using AsyncTask I successfully upload and can retrieve images. I want to use AsyncTask because in my app two activities are using network permissions or if someone has another solution to do this please tell. Code of the Activity:

package com.example.faceb; import java.io.ByteArrayOutputStream; import java.io.File; import com.parse.GetDataCallback; import com.parse.ParseException; import com.parse.ParseFile; import com.parse.ParseObject; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class ListActivity extends Activity { ImageView image; TextView value; public Bitmap bm; public byte[] bitmapdata; public ByteArrayOutputStream data; String s = "FileName"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); uploadTask task = new uploadTask(); task.execute(s); } private class uploadTask extends AsyncTask<String, Void, String>{ @Override protected void onPostExecute(String result) { image.setImageBitmap(bm); } @Override protected String doInBackground(String... params) { upload(); return null; } } public void upload (){ bm = BitmapFactory.decodeResource(getResources(), R.drawable.back); data = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 40, data); bitmapdata = data.toByteArray(); ParseFile file = new ParseFile("hai.png", bitmapdata); file.saveInBackground(); ParseObject testObject = new ParseObject("TestObject"); testObject.put("puu", file); testObject.saveInBackground(); } } 

Following is my error logcat

10-12 01:10:46.944: E/ActivityThread(2233): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider 10-12 01:11:07.604: E/AndroidRuntime(2233): FATAL EXCEPTION: main 10-12 01:11:07.604: E/AndroidRuntime(2233): java.lang.NullPointerException 10-12 01:11:07.604: E/AndroidRuntime(2233): at com.example.faceb.ListActivity$uploadTask.onPostExecute(ListActivity.java:44) 10-12 01:11:07.604: E/AndroidRuntime(2233): at com.example.faceb.ListActivity$uploadTask.onPostExecute(ListActivity.java:1) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.os.AsyncTask.finish(AsyncTask.java:417) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.os.AsyncTask.access$300(AsyncTask.java:127) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.os.Handler.dispatchMessage(Handler.java:99) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.os.Looper.loop(Looper.java:123) 10-12 01:11:07.604: E/AndroidRuntime(2233): at android.app.ActivityThread.main(ActivityThread.java:4627) 10-12 01:11:07.604: E/AndroidRuntime(2233): at java.lang.reflect.Method.invokeNative(Native Method) 10-12 01:11:07.604: E/AndroidRuntime(2233): at java.lang.reflect.Method.invoke(Method.java:521) 10-12 01:11:07.604: E/AndroidRuntime(2233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-12 01:11:07.604: E/AndroidRuntime(2233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-12 01:11:07.604: E/AndroidRuntime(2233): at dalvik.system.NativeStart.main(Native Method) 10-12 01:11:09.014: E/ActivityThread(2274): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider 10-12 01:48:14.084: E/ActivityThread(2313): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider 10-12 01:48:45.323: E/ActivityThread(2334): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider 10-12 03:19:06.184: E/ActivityThread(2371): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider 10-12 03:19:31.614: E/ActivityThread(2393): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider

5
  • Post your stack trace. Also post saveInBackground. Commented Oct 12, 2012 at 15:09
  • stack trace means logcat? And saveInBackground is method provided by parse server. its built in method not created by me. Also when i start this activity i returned to previous activity. Doing this without asynctask in a separate project everything works fine. Commented Oct 12, 2012 at 15:14
  • I don't know anything about facebook apis, but it looks like your problem comes from that. Specifically the inability to find whatever provider is needed for the apis to work. Try verifying your library links and refresh (clean) your project. Commented Oct 12, 2012 at 15:22
  • put this code to the end of the onCreate method: image = this.findViewById(R.id.id_of_your_image_control);; Commented Oct 12, 2012 at 17:30
  • thanks for your suggestion but i dont think that it is the problem of image. Commented Oct 12, 2012 at 17:44

2 Answers 2

2

Using AsyncTask has some limitations that aren't immediately obvious. Its intention is to give developers an easy way to put a slow task in the background, but still allow the result to populate the UI. However, depending on what version of Android you're on, the details of that can have drastic differences because in some versions, all AsyncTask subclasses in your application share the same thread. In your case, I'm assuming you're listening on a port to receive images - and that you're using an AsyncTask to do it. You've got a thread that's always running, always keeping all other AsyncTask threads in your app from running.

Instead, try using Runnable and Handler to get around it.

public class TCPClient implements Runnable { Handler mHandler; Object[] params; public static void run(Handler handler, Object... params) { TCPClient instance = new TCPClient(handler, params); Thread worker = new Thread(instance); worker.start(); } private TCPClient(Handler handler, Object... params) { this.params = params; mHandler = handler; } private void runOnUI(Object result) { if (mHandler != null) { final Object copy = result; mHandler.post(new Runnable() { public void run() { // Update UI here } }); } } public void run() { // Do slow/background task here //Then call runOnUI(new String("resulting data...")); } 

You can use this in a UI thread somewhere like this:

public void onClick(View v) { Handler handler = new Handler(); TCPClient.run(handler, new String("Put outgoing data here..."); } 
Sign up to request clarification or add additional context in comments.

Comments

1

From your stacktrace, onPostExecute is throwing a NullPointerException. From your code this must be because image is null. You create the variable:

ImageView image; 

but I can't see anywhere where you initialise it. Perhaps you are missing a findViewById call in onCreate to set it?

5 Comments

I corrected the point you mentioned but my problem is not solved. My problem is when this activity starts app returns automatically returns to the previous activity
So you got a NullPointerException before, what error do you get in the log now?
I think error is same as before I have updated the logcat. I have run my code without onPostExecute() but error is same it returns to previous activity
Have you tested to see if image is null?
thanks i have solved my problem there was some sdk's problem so i changed that in manifest and above code is working fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.