0

我在保存从 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();
}
4

1 回答 1

1
Here is the complete solution of your problem.You can not do anything like protecting the camera folder to save images. But you can delete the latest image jsut after your image inserted inside the database.

Here is the code for deleting the latest file from the camera folder, Just do copy and paste it and call this method whenever you want to delete the latest file from you camera folder..


private void deleteLatest() {
        // TODO Auto-generated method stub
        File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera" );

        //Log.i("Log", "file name in delete folder :  "+f.toString());
        File [] files = f.listFiles();

        //Log.i("Log", "List of files is: " +files.toString());
        Arrays.sort( files, new Comparator<Object>()
                {
            public int compare(Object o1, Object o2) {

                if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                    //         Log.i("Log", "Going -1");
                    return -1;
                } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                    //     Log.i("Log", "Going +1");
                    return 1;
                } else {
                    //     Log.i("Log", "Going 0");
                    return 0;
                }
            }

                });

        //Log.i("Log", "Count of the FILES AFTER DELETING ::"+files[0].length());
        files[0].delete();

    }

If still you have any query then let me know!!!
Cheers...
于 2012-07-25T05:36:07.607 回答