2012-07-25 80 views
0

我在保存从LayoutView获取的位图图像时出现了一个奇怪的问题。下面的代码创建位图并将其作为图像导出到SD卡上的调色板文件夹。它实际上是这样做的,但是当我打开Gallery应用程序时,我的Camera文件夹中还有一个位图副本。有没有一种方法可以直接复制到文件夹中,而无需在“相机”文件夹中进行复制?此外,如果有一种方法可以删除相机文件夹中的相同副本,那也可以。谢谢!位图复制到相机文件夹

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(); 
} 
+0

Arzeimuth,如果你有你的problem.then的解决方案将其标记为接受。 – 2012-07-25 07:49:44

回答

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... 
+0

这正是我正在寻找的。谢谢尼克! – Arzeimuth 2012-07-25 16:54:58

+0

其实在做更多的调试时,我发现了一个小问题。您的排序和删除方法可以正常工作,并且在查看文件资源管理器时,该文件将在相机文件夹中删除。但是,相机文件夹中的图库中仍然存在缩略图预览。如何删除缩略图?再次感谢! – Arzeimuth 2012-07-25 18:35:42

+0

嗨Arzeimuth,当我这样做时,我没有面临任何问题,但让我检查1ce agn,如果我得到这个解决方案。我一定会让你知道的。 – 2012-07-26 02:48:29

相关问题