如何在共享图像后从SD卡中删除图像

时间:2013-03-29 06:12:32

标签: android

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) 
        {
        case TAKE_IMAGE:
            try {
                if (resultCode == RESULT_OK) {
                    // we need to update the gallery by starting MediaSanner service.
                    mScanner = new MediaScannerConnection(ProfilePicFromCamera.this,new MediaScannerConnection.MediaScannerConnectionClient()
                    {
                       public void onMediaScannerConnected() {
                           mScanner.scanFile(imageUri.getPath(),null /*mimeType*/);
                    }           
                            public void onScanCompleted(String path, Uri uri) {
                            if (path.equals(imageUri.getPath())) {
                                mScanner.disconnect();
                                ProfilePicFromCamera.this.runOnUiThread(new Runnable() {
                                        public void run() {
                                            updateUI();
                                    }
                                });
                            }
                        }
                    });
                    mScanner.connect();

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case UPLOAD_IMAGES:
             if(resultCode==RESULT_OK)
               {       
                        if(file.exists())
                        file.deleteOnExit();  
               }
             break;     }
    }

3 个答案:

答案 0 :(得分:2)

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

           if (requestCode == CAMERA_REQUEST  && resultCode == getActivity().RESULT_OK)       {
                   File file = new File(mCapturedImagePath);
                   file.delet();
            }
    }

or  u can use only this(as your condition)
File file = new File(mCapturedImagePath);
file.delet();

答案 1 :(得分:0)

File file = new File(Environment.getExternalStorageDirectory()+"image_name"); //if your image is directly inside sdcard
file.delete();

答案 2 :(得分:0)

使用以下代码可能对您有所帮助。

File fdelete = new File(file_dj_path);
    if (fdelete.exists()) {
        if (fdelete.delete()) {
            System.out.println("file Deleted :" + file_dj_path);
        } else {
            System.out.println("file not Deleted :" + file_dj_path);
        }
    }

删除图片后刷新图库的代码

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
 Uri.parse("file://" +  Environment.getExternalStorageDirectory())));

检查一下:https://stackoverflow.com/a/10716773/1381827