在DCIM /公用文件夹的Andr​​oid prevent节约照片 [英] Android prevent saving photo on DCIM / public folder

查看:160
本文介绍了在DCIM /公用文件夹的Andr​​oid prevent节约照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm开发Android应用程序。用户可能需要点击一个按钮后的照片。这张照片将被保存到内部包存储(数据/数据​​/包/ ...)用下面的方法:

I´m developing an Android application. The user could take a photo after a button click. This photo will be saved to internal package storage (data/data/package/...) with the following method:

private String saveToInternalSorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("TEST", Context.MODE_PRIVATE);
    File pod = new File(directory, object.getTitle() + "" +
object.getName() + "" + object.getAge() + ".jpg");

    FileOutputStream fos = null;
    try {           

        fos = new FileOutputStream(pod);
        bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pod.getAbsolutePath();
} 

也可以汇入作业中删除从该目录的图片。这就像一个魅力。测试在仿真器和扎根电话。但照片也被保存到公用文件夹DCIM。与HTC ONE迷你I'm测试(withtout SD卡?)。下面是code足见其方法取并获得照片。

Also it´s possible to delete the picture from this directory. This works like a charm. Tested on Emulator and rooted phone. But the photos were also saved to the public folder DCIM. I´m testing with HTC ONE mini (withtout SD CARD?). Below is the code which shows the methods to take and get the photos.

public void takePhoto() {

    cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(cameraintent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {

             Bundle extras = data.getExtras();
             Bitmap bmp  = (Bitmap) extras.get("data");

             setPath(saveToInternalSorage(bmp));

我想prevent的照片存储在公共文件夹中。我以删除DCIM文件夹中最新的文件的方法失败,因为 getExternalStorageDirectory()给了我一个模拟路径(如模拟/ SD卡/ ...)上的HTC One迷你。并that's definitly不正确的路径。所以,我怎么能确定这照片会在一个公共文件夹中只存储到内部封装结构,而不是(无SD卡/带SD卡)。而当我在公众文件夹中删除的照片怎么做我得到正确的路径(/不同设备上的)?

I would like to prevent the storage of the photos in a public folder. My approach to delete the latest files in the DCIM folder failed because getExternalStorageDirectory() gives me a emulated path (like emulated/sdcard/...) on HTC One mini. And that´s definitly not the correct path. So how could i be sure that photos will be only stored to the internal package structure and not (without SD card/ with SD card) in a public folder. And when i have to delete photos in the public folder how to do i get the right path (for/on different devices)?

我发现没有解决方案,以prevent存储在从一开始就公共文件夹。

I found no solution to prevent the storage in a public folder "from the beginning".

在此先感谢!

修改

下面的方法应该能够删除从DCIM /公用文件夹的照片。

The method below should be able to delete the photo from the DCIM/ public folder.

private void deleteLatestFromDCIM() {

    File f = new File(Environment.getExternalStorageDirectory() + "");

    File [] files = f.listFiles();

    Arrays.sort( files, new Comparator<Object>()
            {
        public int compare(Object o1, Object o2) {

            if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                   return -1;
            } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                   return 1;
            } else {
                   return 0;
            }
            ...
    if(files[0].exists())
    files[0].delete();

的问题是,在DCIM /公用文件夹照片得到的通用名称。见下图:

The problem is that photos in DCIM/public folder get generic names. See image below:

那么,如何删除其名字我不知道的图像?在内部存储器中的照片保存工作正常!我不惯于他们在一个公共文件夹中。并与getExternalStorageDirectory()方法,如上文所述,我得到一个模拟的路径。这是真正的路径DCIM /公用文件夹?

So, how to delete images whose names i don´t "know"? Storing of photos in internal memory works fine! I don´t wont them in a public folder. And with the getExternalStorageDirectory() method i get an emulated path as described above. Is this really the path to the DCIM/public folder?

推荐答案

对不起,回答我的问题,希望这将是有益的其他开发人员:
我的策略:拍摄照片中的问题描述,将它保存到内部存储器(数据/数据​​/ com.package ...)
之后,从公用文件夹(DCIM /媒体/ 100MEDIA)用下面的方法删除它(从文件夹中删除最后拍摄的照片...):

Sorry for answering my own question, hope this will be helpfully for other developers: My strategy: Capture the photo as described in the question, save it into the internal memory (data/data/com.package...) After that delete it from the public folder (DCIM/MEDIA/100MEDIA) with the following method (delete last taken picture from that folder...):

    private void deleteLastPhotoTaken() {

    String[] projection = new String[] {
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE };

    final Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, 
                            null,null, MediaStore.Images.ImageColumns.DATE_TAKEN + "  
                            DESC");

    if (cursor != null) {
        cursor.moveToFirst();

        int column_index_data =  
                    cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                     String image_path = cursor.getString(column_index_data);

        File file = new File(image_path);
        if (file.exists()) {
            file.delete();

正如在其他解决方案中提到:鸵鸟政策关闭游标! Android将会为你做。

As mentioned in other solutions: Don´t close the cursor! Android will do that for you.

这篇关于在DCIM /公用文件夹的Andr​​oid prevent节约照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆