我在保存从 LayoutView 获得的位图图像时遇到了一个奇怪的问题。下面的代码创建位图并应作为图像导出到 SD 卡上的调色板文件夹。它实际上是这样做的,但是当我打开 Gallery 应用程序时,我的相机文件夹中也有位图的副本。有没有办法我可以单独复制到文件夹中而不在相机文件夹中复制?此外,如果有一种方法可以删除“相机”文件夹中的相同副本,那也可以。谢谢!
LinearLayout paletteView = (LinearLayout)findViewById(R.id.ExportBitmapLayout);
paletteView.setDrawingCacheEnabled(true);
paletteView.buildDrawingCache();
Bitmap bm = paletteView.getDrawingCache();
String filename = "PALETTETITLE";
filename.toLowerCase();
filename.replace(" ","_");
filename = filename.substring(0, (filename.length() < 30) ? filename.length() : 29);
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.TITLE, filename);
boolean created = createDirIfNotExists("palettes");
if (created)
Log.i("Directory created","OK");
else
Log.i("Directory exists","OK");
//if successful
try
{
OutputStream fOut = null;
String longString = Environment.getExternalStorageDirectory()
+ File.separator + "palettes" +File.separator + filename+".jpg";
File f = new File(longString);
fOut = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), filename, filename);
Context context = ExportMenuImageActivity.this.getApplicationContext();
Toast toast = Toast.makeText(context, "Image saved to Gallery! NOTE: Unintentional save in Camera folder. Fix in next update.", Toast.LENGTH_LONG);
toast.show();
MediaScannerConnection.scanFile(context, new String[] {longString}, null, new MediaScannerConnection.OnScanCompletedListener()
{
public void onScanCompleted(String path, Uri uri)
{
}
});
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}