Android error onBackPressed with deleteDirectoryTree method

你。 提交于 2019-12-13 07:26:58

问题


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

来源:https://stackoverflow.com/questions/33657619/android-error-onbackpressed-with-deletedirectorytree-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!