简体   繁体   English

从相机捕获图像,它存储在sdcard / dcim文件夹中

[英]Capturing image from Camera, it stores at sdcard/dcim folder

I am capturing image by using camera in my app. 我正在使用我的应用程序中的相机捕获图像。

1) it saves image at sdcard, with the name which i passed in intent. 1)它将图像保存在sdcard中,其名称是我故意传递的。

2) also saving the image at sdcard/dcim folder 2)还将图像保存在sdcard / dcim文件夹中

I do not want camera to save image at location mentioned in # 2. i am deleting image from the location in #1. 我不希望相机将图像保存在#2中提到的位置。我要从#1中的位置删除图像。 and want to delete image from location in #2 too. 并且也想从#2中的位置删除图像。

Below is the code snippet for capturing image. 以下是用于捕获图像的代码段。

    SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tempImage.jpg";
    file =new File(SD_CARD_TEMP_DIR);
    Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA);

this Thread helped me to solve this problem. 这个主题帮助我解决了这个问题。 Now what I am doing, 1) capture image, 2) read last image in Media uri, and delete that. 现在我在做什么,1)捕获图像,2)读取Media uri中的最后一张图像,然后将其删除。

private void FillPhotoList() {  
// initialize the list!    
GalleryList.clear();    
String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME }; 
for(int i=0;i<projection.length;i++)
    Log.i("InfoLog","projection "+projection[0].toString());
// intialize the Uri and the Cursor, and the current expected size.    
Cursor c = null;     
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
Log.i("InfoLog","FillPhoto Uri u "+u.toString());
// Query the Uri to get the data path.  Only if the Uri is valid.    
if (u != null)    
{       
    c = managedQuery(u, projection, null, null, null);    
}     
// If we found the cursor and found a record in it (we also have the id).    
if ((c != null) && (c.moveToFirst()))     
{       
    do        
    {         
        // Loop each and add to the list.         
        GalleryList.add(c.getString(0)); // adding all the images sotred in the mobile phone(Internal and SD card)

    }            
    while (c.moveToNext());    
} 
Log.i(INFOLOG,"gallery size "+ GalleryList.size());
      } 


public void movingCapturedImageFromDCIMtoMerchandising()
  {

// This is ##### ridiculous.  Some versions of Android save         
// to the MediaStore as well.  Not sure why!  We don't know what        
// name Android will give either, so we get to search for this         
// manually and remove it.           
String[] projection = { MediaStore.Images.ImageColumns.SIZE, 
        MediaStore.Images.ImageColumns.DISPLAY_NAME, 
        MediaStore.Images.ImageColumns.DATA, 
        BaseColumns._ID,}; 
// intialize the Uri and the Cursor, and the current expected size.  

for(int i=0;i<projection.length;i++)
    Log.i("InfoLog","on activityresult projection "+projection[i]);
//+" "+projection[1]+" "+projection[2]+" "+projection[3] this will be needed if u remove the for loop
Cursor c = null;          
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;       
Log.i("InfoLog","on activityresult Uri u "+u.toString());

if (CurrentFile != null)      
{                          
    // Query the Uri to get the data path.  Only if the Uri is valid,     
    // and we had a valid size to be searching for.     
    if ((u != null) && (CurrentFile.length() > 0))        
    {              
        //****u is the place from data will come and projection is the specified data what we want
        c = managedQuery(u, projection, null, null, null);      
    }           
    // If we found the cursor and found a record in it (we also have the size). 
    if ((c != null) && (c.moveToFirst()))     
    {             
        do              
        {                
            // Check each area in the gallery we built before.     
            boolean bFound = false;               
            for (String sGallery : GalleryList)                  
            {                      
                if (sGallery.equalsIgnoreCase(c.getString(1)))  
                {                      
                    bFound = true;
                    Log.i("InfoLog","c.getString(1) "+c.getString(1));
                    break;                    
                }                   
            }                   
            // To here we looped the full gallery.                   
            if (!bFound)     //the file which is newly created and it has to be deleted from the gallery              
            {                     
                // This is the NEW image.  If the size is bigger, copy it.          
                // Then delete it!                    
                File f = new File(c.getString(2));




                // Ensure it's there, check size, and delete!            
                if ((f.exists()) && (CurrentFile.length() < c.getLong(0)) && (CurrentFile.delete()))     
                {                       
                    // Finally we can stop the copy.       
                    try                      
                    {                         
                        CurrentFile.createNewFile();     
                        FileChannel source = null;   
                        FileChannel destination = null; 
                        try                           
                        {                         
                            source = new FileInputStream(f).getChannel();
                            destination = new FileOutputStream(CurrentFile).getChannel();  
                            destination.transferFrom(source, 0, source.size());
                        } 
                        finally                    
                        {
                            if (source != null)        
                            {   
                                source.close();  
                            }       
                            if (destination != null)   
                            {   
                                destination.close(); 
                            }                            
                        }                     
                    }                         
                    catch (IOException e)                 
                    {                            
                        // Could not copy the file over.      
                        ToastMaker.makeToast(this, "Error Occured", 0);   
                    }                      
                }                   
                //****deleting the file which is in the gallery                           
                Log.i(INFOLOG,"imagePreORNext1 "+imagePreORNext);
                Handler handler = new Handler();
                //handler.postDelayed(runnable,300);
                Log.i(INFOLOG,"imagePreORNext2 "+imagePreORNext);
                ContentResolver cr = getContentResolver();       
                cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=" + c.getString(3), null);

                break;                                          
            }              
        }            
        while (c.moveToNext());   
    }         
 }       

 }

You can use the following: First we get the last saved image by checking which was the last modified image. 您可以使用以下内容:首先,通过检查哪个是最后修改的图像来获取最后保存的图像。 Than simply delete it. 比简单地删除它。

private boolean deleteLastFromDCIM() {

        boolean success = false;
        try {
            File[] images = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "DCIM/Camera").listFiles();
            File latestSavedImage = images[0];
            for (int i = 1; i < images.length; ++i) {
                if (images[i].lastModified() > latestSavedImage.lastModified()) {
                    latestSavedImage = images[i];
                }
            }

                   // OR just use success = latestSavedImage.delete(); 
            success = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "DCIM/Camera/"
                    + latestSavedImage).delete();
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return success;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android 11的外部SDCARD的DCIM文件夹中保存图像? - how to save image in DCIM folder of external SDCARD in android 11? 如何从Android的SD卡中的DCIM / Camera文件夹中检索最后保存的图像 - How to retrieve the last saved image from DCIM/Camera folder in sd card in Android 无法访问 /mnt/sdcard/dcim/camera/ - cannot access /mnt/sdcard/dcim/camera/ Android外部sdCard DCIM文件夹路径 - Android external sdCard DCIM folder path 如何从意图打开默认文件应用程序中的 DCIM/Camera 文件夹 - How to open DCIM/Camera folder in default Files app from intent 相机拍摄的两张图像,无法从DCIM / Camera文件夹中删除一张 - Two images captured by camera and not able to delete one from DCIM/Camera folder 将图像从可绘制图像保存到相机活动中的sdcard - Save Image from drawable to sdcard in camera activity 在没有SD卡的情况下从本机相机拍摄图像 - Taking image from native camera without a sdcard 如何将默认保存文件夹DCIM \\ Camera更改为任何文件夹名称并使用MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA - How to change default save folder DCIM\Camera to any folder name and used MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA 如何在/ DCIM / Camera上获取图像文件数组? - How to get array of image files on /DCIM/Camera?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM