ShareIntent crashing app

  • Replies:5
  • OpenNot stickiedAnswered
  • Forum posts: 6

Jul 30, 2015 9:37:46 PM via Website

I open images fullscreen from a productDetails activity. This new fullScreeActivity is working fine in the first try. Problem is happening when I press android native back button to return to productDetails and so try to open fullScreeActivity with the same image (just like if I´d like to see the image full again).

Logcat report:

07-30 18:51:10.294: E/AndroidRuntime(1866): FATAL EXCEPTION: main
07-30 18:51:10.294: E/AndroidRuntime(1866): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.habitodigital.handy/com.habitodigital.handy.FullScreenActivity}: java.lang.NullPointerException
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.os.Looper.loop(Looper.java:137)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at dalvik.system.NativeStart.main(Native Method)
07-30 18:51:10.294: E/AndroidRuntime(1866): Caused by: java.lang.NullPointerException
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.habitodigital.handy.FullScreenActivity.createShareIntent(FullScreenActivity.java:99)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.habitodigital.handy.FullScreenActivity.access$0(FullScreenActivity.java:91)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.habitodigital.handy.FullScreenActivity$1.onSuccess(FullScreenActivity.java:74)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.squareup.picasso.RequestCreator.into(RequestCreator.java:514)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at com.habitodigital.handy.FullScreenActivity.onCreate(FullScreenActivity.java:66)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.Activity.performCreate(Activity.java:5008)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-30 18:51:10.294: E/AndroidRuntime(1866):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

productDetails:

ImageView arquivo;
private static final String TAG_ARQUIVO = "arquivo";
String imagem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_details);

  DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    .showImageOnFail(R.drawable.no_image_available)
    .showImageForEmptyUri(R.drawable.no_image_available)
    .cacheInMemory(true)
    .imageScaleType(ImageScaleType.EXACTLY)
    .displayer(new FadeInBitmapDisplayer(300)).build();

  arquivo = (ImageView) findViewById(R.id.arquivo);

  Intent i = getIntent();
  imagem = i.getStringExtra(TAG_ARQUIVO);

  ImageLoader imageLoader = ImageLoader.getInstance();
  imageLoader.displayImage(imagem, arquivo, defaultOptions);

  arquivo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent iFullImg = new Intent(getApplicationContext(), FullScreenActivity.class);
            iFullImg.putExtra(TAG_ARQUIVO, imagem);
            startActivity(iFullImg);

        }
    });
 }

fullScreenActivity:

    PhotoViewAttacher mAttacher;
private ShareActionProvider mShareActionProvider;
private static final String TAG_ARQUIVO = "arquivo";
String path;
File file;

   ImageView fullImg;

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    .showImageOnFail(R.drawable.no_image_available)
    .showImageForEmptyUri(R.drawable.no_image_available)
    .cacheInMemory(true)
    .imageScaleType(ImageScaleType.EXACTLY)
    .displayer(new FadeInBitmapDisplayer(300)).build();

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    fullImg = (ImageView) findViewById(R.id.fullImage);


    Intent i = getIntent();
    path = i.getStringExtra(TAG_ARQUIVO);

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(path, fullImg, defaultOptions);

    mAttacher = new PhotoViewAttacher(fullImg);


    Picasso.with(this).load(path).into(fullImg, new Callback(){
        @Override
        public void onError() {

        }

        @Override
        public void onSuccess() {
            createShareIntent();
        }
    });

}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.full_screen, menu);

    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    return true;
}

    private void createShareIntent() {

    Uri bmpUri = getLocalBitmapUri(fullImg);

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/*");
    mShareActionProvider.setShareIntent(shareIntent);


}

  // Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
       bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
       return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(  
            Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

When I remove createShareIntent() and Picasso codes it works fine. So I suppose it is what´s causing the problem. Any change I could do to make it work?

Pascal P.
  • Mod
  • Blogger
  • Forum posts: 8,583

Jul 31, 2015 3:14:35 AM via App

You get an NPE so I think you didn't initialize the ShareActionProvider: shareIntent.setType("image/*");
mShareActionProvider.setShareIntent(shareIntent);

Thats why it's null and thriws an Exception.

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 6

Jul 31, 2015 7:35:20 AM via Website

Pascal P.

You get an NPE so I think you didn't initialize the ShareActionProvider: shareIntent.setType("image/*");
mShareActionProvider.setShareIntent(shareIntent);

Thats why it's null and thriws an Exception.

But it happens just in the second try when I press native back button to return to detaiilsActivity and try to open the same image fullsize again from the same detaiilsActivity (kinda repeating the process)

Pascal P.
  • Mod
  • Blogger
  • Forum posts: 8,583

Jul 31, 2015 10:45:03 AM via Website

You have two options:
First ist, creating an ShareActionProvider as DropDownMenu like here: http://wptrafficanalyzer.in/blog/using-shareactionprovider-in-action-bar/ [user can share an action]
Second is, instantly send the share Intent without Provider, then the user "should" share an action.

LG Pascal //It's not a bug, it's a feature. :) ;)

  • Forum posts: 6

Jul 31, 2015 6:06:06 PM via Website

this is not the problem... I have the DropDownMenu working fine.. the problem is the when I try to open the image full for the second time, after pressing android native back button

  • Forum posts: 6

Jul 31, 2015 11:09:00 PM via Website

After some modifications and simplifications that I did, the NPE was solved.

public class FullScreenActivity extends Activity {

PhotoViewAttacher mAttacher;
ShareActionProvider mShareActionProvider;
private static final String TAG_ARQUIVO = "arquivo";
String path;
File file;
Intent shareIntent;
ImageView fullImg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

fullImg = (ImageView) findViewById(R.id.fullImage);

Intent i = getIntent();
path = i.getStringExtra(TAG_ARQUIVO);

Picasso.with(FullScreenActivity.this).load(path).into(fullImg);
mAttacher = new PhotoViewAttacher(fullImg);

Uri bmpUri = getLocalBitmapUri(fullImg);

shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.full_screen, menu);

MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
if (mShareActionProvider != null) {
    mShareActionProvider.setShareIntent(shareIntent);
}
return true;
}

public Uri getLocalBitmapUri(ImageView imageView) {

Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
   bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
   return null;
}

Uri bmpUri = null;
try {
    File file =  new File(Environment.getExternalStoragePublicDirectory(  
        Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
    file.getParentFile().mkdirs();
    FileOutputStream out = new FileOutputStream(file);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.close();
    bmpUri = Uri.fromFile(file);
} catch (IOException e) {
    e.printStackTrace();
}
return bmpUri;
}

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
  int id = item.getItemId();

  if (id == R.id.action_settings) {
     return true;
   }
    return super.onOptionsItemSelected(item);
  }
}

Thanks for the attention.