我有一个聊天应用程序,我需要保存用户发送的图像。对于用户发送的图像,我将其保存到图像文件夹中,如此
private void imageDownload(final String url){
Picasso.with(getContext())
.load(url)
.into(getTarget(url));
}
//target to save
private static Target getTarget(final String url){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File heyJudeFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Hey Jude");
if (!heyJudeFile.exists()){
heyJudeFile.mkdirs();
}
localProfilePictureAdress = Environment.getExternalStorageDirectory().getPath() + "/Hey Jude" + "/" + url.substring(url.lastIndexOf("/")+1);
File file = new File(localProfilePictureAdress);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
return target;
}
但对于用户收到的图像,我想保存图像,但我不希望图像在图库中可见,这是如何工作的?
由于
答案 0 :(得分:1)
使用以下方法隐藏图库中的媒体
/* To Hide media file in gallery */
public void createNoMedia(String myDir){
File noMediaFile = new File(myDir, ".nomedia");
if (!noMediaFile.exists()) {
try {
noMediaFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* To use this */
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MyImages/";
File newDir = new File(dir);
newDir.mkdirs();
createNoMedia(dir);
并在manifest.xml
您还需要WRITE_EXTERNAL_STORAGE
权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
您可以通过编辑文件名称并添加未知格式来实现,例如&#34; .example&#34;然后它不会出现在画廊