在android sdcard donot工作中重命名文件名

时间:2015-07-04 18:32:26

标签: android android-sdcard

您好我尝试选择图像并重命名并将其设置为在图像视图中显示。 但问题是,当我重命名它时,如果我不重命名文件,它就说“文件不存在”,这是正常的。

这是Get Image的Activity结果中的代码是sdcard:

Uri selectedImage = data.getData();

        Log.e("URI Get Path", selectedImage.getPath());

        String[] filePathColumn = { MediaStore.Images.Media.DATA };



        Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);

        Log.e("Picture Path 1 ", picturePath);

        String currentFileName = picturePath.substring(picturePath.lastIndexOf("/") + 1);

        Log.e("Current File Name",currentFileName);

        File sdcard = new File(picturePath.substring(0, picturePath.lastIndexOf("/")));
        File from = new File(sdcard,currentFileName);

        Calendar c = Calendar.getInstance();

        File to;

        if(currentFileName.contains("_"))
            to = new File(sdcard,User_Info.user_id + "___" + String.valueOf(c.getTimeInMillis()) + "__" +  currentFileName.substring(currentFileName.lastIndexOf("_") + 1) );
        else
            to = new File(sdcard,User_Info.user_id + "___" + String.valueOf(c.getTimeInMillis()) + "__" +  currentFileName);

        if(from.exists())
            from.renameTo(to);
        else
            Log.e("from not exist", from.getAbsolutePath());

        Log.e("From After Change", from.getAbsolutePath());
        Log.e("To After Change", to.getAbsolutePath());

        if(to.exists())
            picturePath = to.getAbsolutePath();
        else
            Log.e("to not exist", to.getAbsolutePath());

        cursor.close();

        Log.e("picturepath", picturePath);

这是Log:

07-04 23:11:11.730: E/URI Get Path(32250): /external/images/media/13031
07-04 23:11:11.770: E/Picture Path 1(32250): /mnt/sdcard/156___1435700963195__2006112342.jpg
07-04 23:11:11.780: E/Current File Name(32250): 156___1435700963195__2006112342.jpg
07-04 23:11:11.820: E/from not exist(32250): /mnt/sdcard/156___1435700963195__2006112342.jpg
07-04 23:11:11.820: E/From After Change(32250): /mnt/sdcard/156___1435700963195__2006112342.jpg
07-04 23:11:11.820: E/To After Change(32250): /mnt/sdcard/156___1436037071817__2006112342.jpg
07-04 23:11:11.830: E/to not exist(32250): /mnt/sdcard/156___1436037071817__2006112342.jpg
07-04 23:11:11.840: E/picturepath(32250): /mnt/sdcard/156___1435700963195__2006112342.jpg
07-04 23:11:11.860: E/BitmapFactory(32250): Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/156___1435700963195__2006112342.jpg: open failed: ENOENT (No such file or directory)

1 个答案:

答案 0 :(得分:0)

重命名文件并再次检查源文件!你必须检查重命名的文件! 实际上这一行

if(from.exists())

始终返回false,因为在上一行中您重命名了文件。

from.renameTo(to);