下面的代码将图片加载到我的应用程序中的全屏活动中。它们来自listview,然后加载到details_activity中,因此我将它们打开到全屏活动中。
我注意到只有当我全屏打开它们时才会存储到SD卡存储中。那么,有人可以告诉我,在返回details_activity或关闭应用程序后删除图像的最佳方法是什么?
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;
}
// 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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
修改
当我按下android native back按钮时,我尝试使用下面的代码来删除图像,但它不起作用:
PicassoTools.java
public class PicassoTools {
public static void clearCache(Picasso p) {
p.cache.clear();
}
}
FullScreen.java:
@Override
public void onBackPressed() {
PicassoTools.clearCache(Picasso.with(FullScreenActivity.this));
super.onBackPressed();
}
答案 0 :(得分:0)
我将clearCache更改为deleteDirectoryTree(file):
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 void onBackPressed() {
deleteDirectoryTree(file);
super.onBackPressed();
}
@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);
}
public static void deleteDirectoryTree(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteDirectoryTree(child);
}
}
fileOrDirectory.delete();
}
}
已经做了一些测试,现在它正在运行。