2012-05-03 9 views
27

私のアプリケーションでは、ビデオを録画する必要があります。録音を開始する前に、私はそれに名前とディレクトリを割り当てています。録音が終わったら、ユーザーはファイルの名前を変更できます。私は次のコードを書いたが、うまくいかないようだ。android、ファイルの名前を変更するには

ユーザーがファイルの名前を入力し、私はこれをやるのボタンをクリックしてください:

private void setFileName(String text) {  
     String currentFileName = videoURI.substring(videoURI.lastIndexOf("/"), videoURI.length()); 
     currentFileName = currentFileName.substring(1); 
     Log.i("Current file name", currentFileName); 

     File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), MEDIA_NAME); 
     File from  = new File(directory, "currentFileName"); 
     File to  = new File(directory, text.trim() + ".mp4"); 
     from.renameTo(to); 
     Log.i("Directory is", directory.toString()); 
     Log.i("Default path is", videoURI.toString()); 
     Log.i("From path is", from.toString()); 
     Log.i("To path is", to.toString()); 
    } 

テキスト:ユーザーによって入力された名前です。 現在のファイル名は: MEDIA_NAMEを記録する前に私が割り当てられた名前です。フォルダの名前

Logcatはこれを示しています

05-03 11:56:37.295: I/Current file name(12866): Mania-Karaoke_20120503_115528.mp4 
05-03 11:56:37.295: I/Directory is(12866): /mnt/sdcard/Movies/Mania-Karaoke 
05-03 11:56:37.295: I/Default path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/Mania-Karaoke_20120503_115528.mp4 
05-03 11:56:37.295: I/From path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/currentFileName 
05-03 11:56:37.295: I/To path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/hesam.mp4 

任意の提案がいただければ幸いです。

答えて

17

を、ここで

File from = new File(directory, "currentFileName"); 

currentFileNameは、あなたはいけない文字列はこのようにそれをしてみてください"

を使用する必要があり、実際にあります

File from  = new File(directory, currentFileName ); 
            ^   ^  //You dont need quotes 
+2

ああ、私の神!何が愚かな間違いだった私はそれをした!!!! Sanjayさん、ありがとうございます。今、私はそれを変更した後にうまく動作します。 – Hesam

+7

@Hesam時にはそのような愚かな間違いが私たちのすべての時間を取る.. :)歓声..喜んでコーディング:) – COD3BOY

+1

笑、誰もが間違いを犯すが、これは本当に面白いものです、ちょうど休憩を取ると、 。 – Krypton

1

異なるファイル名のターゲットファイルオブジェクトを提供します。

// Copy the source file to target file. 
// In case the dst file does not exist, it is created 
void copy(File source, File target) throws IOException { 

    InputStream in = new FileInputStream(source); 
    OutputStream out = new FileOutputStream(target); 

    // Copy the bits from instream to outstream 
    byte[] buf = new byte[1024]; 
    int len; 

    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 

    in.close(); 
    out.close(); 
} 
+1

ファイルが大きい場合は、このアプローチは、 'OutOfMemoryError' ... – nanounanue

+0

@nanounanue挑発可能性:なぜ?一度に1kBを読み書きします。 – Harvey

1

ディレクトリが存在するかどうかを確認する必要があります。あなたのコードで

File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), MEDIA_NAME); 
if(!directory.exist()){ 
    directory.mkdirs(); 
} 
36

が、それはすべきではない:

File from = new File(directory, currentFileName);

代わりの

File from = new File(directory, "currentFileName");


安全のため、

File.renameTo()を使用してください。しかし、名前を変更する前にディレクトリの存在を確認してください!

File dir = Environment.getExternalStorageDirectory(); 
if(dir.exists()){ 
    File from = new File(dir,"from.mp4"); 
    File to = new File(dir,"to.mp4"); 
    if(from.exists()) 
     from.renameTo(to); 
} 

参照してください。問題はこのラインである http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29

+3

私は数ヶ月おきにこのソリューションに戻り続けます。簡潔なソリューションを提供していただきありがとうございます。 <3 – Sipty

2

の作業例...

File oldFile = new File("your old file name"); 
    File latestname = new File("your new file name"); 
    boolean success = oldFile .renameTo(latestname); 

    if(success) 
    System.out.println("file is renamed.."); 
+1

ベスト!投票! – Fortran

3

/** 
* ReName any file 
* @param oldName 
* @param newName 
*/ 
public static void renameFile(String oldName,String newName){ 
    File dir = Environment.getExternalStorageDirectory(); 
    if(dir.exists()){ 
     File from = new File(dir,oldName); 
     File to = new File(dir,newName); 
     if(from.exists()) 
      from.renameTo(to); 
    } 
} 
6

ファイルの名前を変更するために、このメソッドを使用します。ファイルfromの名前はtoに変更されます。

private boolean rename(File from, File to) { 
    return from.getParentFile().exists() && from.exists() && from.renameTo(to); 
} 

例コード:

public class MainActivity extends Activity { 
    private static final String TAG = "YOUR_TAG"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     File currentFile = new File("/sdcard/currentFile.txt"); 
     File newFile new File("/sdcard/newFile.txt"); 

     if(rename(currentFile, newFile)){ 
      //Success 
      Log.i(TAG, "Success"); 
     } else { 
      //Fail 
      Log.i(TAG, "Fail"); 
     } 
    } 

    private boolean rename(File from, File to) { 
     return from.getParentFile().exists() && from.exists() && from.renameTo(to); 
    } 
} 
1

これは私が使用して終了するものです。これは、ファイル名に整数を追加することによって、同じ名前を持つ既存のファイルが存在する場合を処理します。

@NonNull 
private static File renameFile(@NonNull File from, 
           @NonNull String toPrefix, 
           @NonNull String toSuffix) { 
    File directory = from.getParentFile(); 
    if (!directory.exists()) { 
     if (directory.mkdir()) { 
      Log.v(LOG_TAG, "Created directory " + directory.getAbsolutePath()); 
     } 
    } 
    File newFile = new File(directory, toPrefix + toSuffix); 
    for (int i = 1; newFile.exists() && i < Integer.MAX_VALUE; i++) { 
     newFile = new File(directory, toPrefix + '(' + i + ')' + toSuffix); 
    } 
    if (!from.renameTo(newFile)) { 
     Log.w(LOG_TAG, "Couldn't rename file to " + newFile.getAbsolutePath()); 
     return from; 
    } 
    return newFile; 
} 
関連する問題