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);
}
}
file
field somehow. Maybe thegetLocalBitmapUri
function is returning early, or thenew File
line is throwing an exception. – Itinerancyfile
is null before callingdeleteDirectoryTree
. 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