Android error onBackPressed with deleteDirectoryTree method
Asked Answered
T

0

0

The class below opens images full from a GridView thumb click. I am trying to delete the cached images after returning from the fullscreen to gridview activity.

At this point the app gives me an error and returns to gridview activity. I have another app with this method working fine on back button press.

What is the problem here?

DetailsActivity (Fullscreen) class:

public class DetailsActivity extends Activity {

private TouchImageView imageView;
PhotoViewAttacher mAttacher;
ShareActionProvider mShareActionProvider;
File file;
Intent shareIntent;

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

    Intent i = getIntent();
    String image = i.getStringExtra("image");

    //Set image url
    imageView = (TouchImageView) findViewById(R.id.grid_item_image);
    Picasso.with(DetailsActivity.this).load(image).into(imageView);

    Uri bmpUri = getLocalBitmapUri(imageView);

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


}


@Override
public void onBackPressed() {
    deleteDirectoryTree(file);
    super.onBackPressed();
}

public static void deleteDirectoryTree(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) {
        for (File child : fileOrDirectory.listFiles()) {
            deleteDirectoryTree(child);
        }
    }

    fileOrDirectory.delete();
}

@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;
}

// 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 =  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);
 }
}
Twenty answered 11/11, 2015 at 18:43 Comment(7)
What error do you get? Is there a stacktrace in the logs?Itinerancy
@LawrenceKesteloot nullpointerexception error. Then it shows me the line and it goes on if (fileOrDirectory.isDirectory()) lineTwenty
Right, so you never set the file field somehow. Maybe the getLocalBitmapUri function is returning early, or the new File line is throwing an exception.Itinerancy
@LawrenceKesteloot I tried changing the order of the methods in many ways but still got error. By now I made it to create an app folder inside "downloads"..so it keeps easier to delete them if the user needs spaceTwenty
Well you could check whether file is null before calling deleteDirectoryTree. You really need to track this down -- this code isn't doing what you think it is. Methodically figure out what's going wrong.Itinerancy
@LawrenceKesteloot I think it worked. I´ll keep testing it but it works by now. Thanks again!Twenty
@LawrenceKesteloot nope..after some tests if I press back button and I try to open images again the share stops working. I think I need a way to delete the cache folder after some time, like whatsapp does, deleting backup conversation after 24hsTwenty

© 2022 - 2024 — McMap. All rights reserved.