0

What is the wrong that i have done in here. I couldn't figure it out. I have a input method service and my keyboard has images when i click on a image i am trying share the image to other application.

Service Code:

under onCreateInputView Method

mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);



        if(Build.VERSION.SDK_INT > 20){
            mPackageName = this.mActivityManager.getRunningAppProcesses().get(0).processName;
         //Toast.makeText(this,mPackageName, Toast.LENGTH_LONG).show();
        }
        else{
            mPackageName = this.mActivityManager.getRunningTasks(1).get(0).baseActivity.getPackageName();
        }


        shareIntent = new Intent(Intent.ACTION_SEND);

        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        shareIntent.setType("image/png"); 
        shareIntent.setPackage(mPackageName);

Method that is called from onPost of my asynctask

public void sendImage(int position)
    {

        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
        //folder.mkdirs();

        if(folder.exists())
        {
          //Save the path as a string value
          String extStorageDirectory = folder.toString();



            String fileName = MainActivity.imageName.get(position)+".png";

            File dataFile = new File(extStorageDirectory,fileName);

            //Intent shareIntent = new Intent(Intent.ACTION_SEND);


            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
            //Intent new_intent = Intent.createChooser(shareIntent, "Share via");
            //new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(shareIntent);



        }
        else
        {
            Toast.makeText(this, "Not Found", Toast.LENGTH_LONG).show();

        }


    }

Async task:

@Override
    protected String doInBackground(String... params) {

        SaveImage(this.url);


        return null;
    }

       private String SaveImage(String urlstr) {

            String filepath = null;
            try
            {   
              URL url = new URL(urlstr);
              HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setDoOutput(false);
              urlConnection.setRequestMethod("GET");
              //urlConnection.setDoOutput(true);
              urlConnection.setInstanceFollowRedirects(false);
              urlConnection.connect();                  

              File SDCardRoot = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
              SDCardRoot.mkdirs();
              String filename=this.name+".jpg";   
              Log.i("Local filename:",""+filename);
              File file = new File(SDCardRoot,filename);
              if(file.createNewFile())
              {
                file.createNewFile();
              }                 
              FileOutputStream fileOutput = new FileOutputStream(file);
              InputStream inputStream = urlConnection.getInputStream();
              int totalSize = urlConnection.getContentLength();
              int downloadedSize = 0;   
              byte[] buffer = new byte[1024];
              int bufferLength = 0;
              while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
              {                 
                fileOutput.write(buffer, 0, bufferLength);                  
                downloadedSize += bufferLength;                 
                Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
              }             
              fileOutput.close();
              if(downloadedSize==totalSize) filepath=file.getPath();    
            } 
            catch (MalformedURLException e) 
            {
              e.printStackTrace();
            } 
            catch (IOException e)
            {
              filepath=null;
              e.printStackTrace();
            }
            Log.i("filepath:"," "+filepath);

            return filepath;
        }


       @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        if(ct!=null)
        {
//      dialog = new ProgressDialog(ct);
//      dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
//      dialog.setMessage("loading...");
        //dialog.show();
        }



    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //dialog.dismiss();

        this.ct.sendImage(this.pos);

    }   

it give's me exception at sendImage Method in

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));

    startActivity(shareIntent);

logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
at MainActivity.sendImage(MainActivity.java:550)
at 

    ImageDownload.onPostExecute(ImageDownload.java:118)
    ImageDownload.onPostExecute(ImageDownload.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Fay007
  • 2,707
  • 3
  • 28
  • 58
  • 2
    make sure `Uri.fromFile(dataFile)` is not `null` – Rustam May 08 '15 at 12:09
  • Are you sure onCreateInputView is being called before sendImage runs? If you're going to have the intent be the same every time it's safer to initialize it in onCreate or onResume. onCreateInputView is only called when on screen keyboard or other input appears. – Ben May 08 '15 at 12:10
  • it is not null @Rustam – Fay007 May 08 '15 at 12:12
  • Try to initialize shareIntent before this line : shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile)); – Haresh Chhelana May 08 '15 at 12:16
  • i have done that nothing changed @Ben – Fay007 May 08 '15 at 12:20
  • Have you attached a debugger to make sure the initialization of shareIntent is being called before you call putExtra? For whatever reason shareIntent is null when it reaches that line. So you should either place the intent initialization back in that code block or figure out why it's not being called where you have it in your code. It's honestly a little hard to say why that is when only viewing snippets of code. – Ben May 08 '15 at 12:26
  • i commented all the code and just wrote a toast as like this Toast.makeText(this,String.valueOf(position), Toast.LENGTH_LONG).show(); yet it gave me exception ava.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources() @Ben – Fay007 May 08 '15 at 12:33
  • Is the context null? – Ben May 08 '15 at 17:31

0 Answers0