【发布时间】:2018-07-01 22:17:02
【问题描述】:
我编写了这段代码,让我可以创建一个图像表单 url 并保存为文件以进行共享。如何改进此代码以创建临时图像文件,在共享后或关闭应用程序时删除自己?
public void disegnaBitmap(){
Glide
.with(GlideImgActivity.this)
.load(MY_URL)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(getTarget());
}
private SimpleTarget<Bitmap> getTarget(){
SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Log.d(LOG_TAG,"Bitmap caricata");
shareImgTemp(resource);
}
public void onLoadFailed(Exception e, Drawable errorDrawable){
Log.d(LOG_TAG,"Errore di caricamento 1: "+ e.getMessage());
}
};
return target;
}
public void shareImg(Bitmap icon){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
bytes.close();
} catch (IOException e) {
e.printStackTrace();
}
String pathname = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
File f = new File(pathname);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
}
我知道可以用
File.createTempFile("myfile", ".jpg");
但是它将文件保存在缓存目录中,然后我不知道如何共享它。 我该怎么办?
【问题讨论】:
标签: android share android-image android-bitmap temporary-files