android - 删除 SD 卡中的图像副本

标签 android image android-sdcard

你好 friend ,我在我的项目中使用了以下代码。

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Camera Test" />
    <ImageView android:id="@+id/camera_image"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

代码(Java文件):

public class ImageInfo extends Activity {
        private static final int CAMERA_PIC_REQUEST = 1111;
    private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImage = (ImageView) findViewById(R.id.camera_image);
        //1
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_CANCELED)   {
            if(requestCode == CAMERA_PIC_REQUEST){
                //2
                System.out.println("Request"+requestCode+"result"+resultCode);
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                //  mImage.setImageBitmap(thumbnail);
                //3
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                //4
                File file = new File(Environment.getExternalStorageDirectory()+"/saved_images/image.jpeg");
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    //5
                    fo.write(bytes.toByteArray());
                    fo.close();
                    Intent intent=new Intent( this,Info.class);
                    startActivity(intent);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }
}

我能够成功地操作相机并拍摄图像,但是在 android 的 DCIM 目录(默认目录)中通过 name_date.jpg 创建了一个额外的图像副本 请帮助删除所有这些文件。 谢谢和问候......

最佳答案

这是您从 dcim 文件夹中删除文件的问题的完整解决方案。

just copy and paste this method. And call it whenever necessary.
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();

    }

关于android - 删除 SD 卡中的图像副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878336/

相关文章:

android - 如何在没有 RecyclerView 的情况下在 android 中使用 PullToRefresh

java - Android(在 Scala 中): StackOverflowError depends on when to start a thread?

javascript - 完成 jQuery 脚本 - 重写 href

Android获取所有可用的存储设备

android - 如何在android中获取外部应用程序数据路径?

android - 如何访问Android设备上的图库文件夹

java - 添加Android按钮以动态查看

android - 如果查询没有结果,Room 会返回什么

jquery 悬停一个 div 并在另一个 div 上显示图像效果

html - 根据 CSS 类在另一个图像之上添加图像