フォルダ名変更
import android.content.Context;
import android.util.Log;
import java.io.File;
public class clsForlderMove {
//戻り値
//0:成功
//1:失敗
//-1:同じ場所なので移動(ファイル名変更)の必要なし
//iResourceId:変更元リソースId
//iResourceId2:変更先リソースId
public int folderMove(Context context, int iResourceId, int iResourceId2)
{
Log.d("iResourceId",String.valueOf(iResourceId));
Log.d("iResourceId2",String.valueOf(iResourceId2));
//移動先が同じ場合は処理の必要なし
if(iResourceId==iResourceId2)
{
Log.d("folderMove","移動先が同じ場合は処理の必要なし");
return -1;
}
//変数宣言
int iResult = 0;
String sPath = "";//変更元
String sPath2 = "";//変更先
//ファイル名取得のクラスをインスタンス化
clsCommon cls = new clsCommon();
//フォルダ名を取得します。
sPath = cls.getStringXmlData(context, iResourceId);
sPath = cls.getPath(context,sPath);
Log.d("sFolderName", sPath);
Log.d("sFolderName2", sPath2);
sPath2 = cls.getStringXmlData(context, iResourceId2);
sPath2 = cls.getPath(context,sPath2);
Log.d("sPath", sPath);
Log.d("sPath2", sPath2);
cls = null;
//ファイルオブジェクトを生成
File folder = null;//変更元
File folder2 = null;//変更先
//移動処理
try
{
folder = new File(sPath);//変更元
folder2 = new File(sPath2);//変更先
Log.d("folderMove","リネーム処理");
if(folder.renameTo(folder2) !=true) iResult = 1;
}
catch (Exception e)
{
Log.d("Error Message",e.getMessage().toString());
}
finally {
folder = null;
folder2 = null;
}
return iResult;
}
}
|
「renameTo」メソッドを使用して移動することによりフォルダ名を変更させています。
(例)
folder = new File(sPath);//変更元
folder2 = new File(sPath2);//変更先
Log.d("folderMove","リネーム処理");
if(folder.renameTo(folder2) !=true) iResult = 1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//フォルダ作成
test1();
//フォルダ削除
test2();
//フォルダ名変更
test3();
//フォルダおよびファイル一覧表示
test4();
}
//フォルダ作成
private void test1()
{
int iResult = 0;
clsCreateFolder cls = new clsCreateFolder();
iResult = cls.folderCreate(this, R.string.con_folder1);
Log.d("iResult", String.valueOf(iResult));
}
//フォルダ削除
private void test2()
{
int iResult = 0;
clsRemoveFolder cls = new clsRemoveFolder();
iResult = cls.removeFolder(this, R.string.con_folder1);
Log.d("iResult", String.valueOf(iResult));
}
//フォルダリネーム
private void test3()
{
int iResult = 0;
clsForlderMove cls = new clsForlderMove();
iResult = cls.folderMove(this, R.string.con_folder2, R.string.con_folder1);
Log.d("iResult", String.valueOf(iResult));
}
//フォルダ一覧取得
private void test4()
{
int iResult = 0;
clsFolderList cls = new clsFolderList();
iResult = cls.printFolderList(this, R.string.con_folder1);
Log.d("iResult", String.valueOf(iResult));
}
}
|
<resources>
<string name="app_name">test24</string>
<string name="con_folder1">folder1</string>
<string name="con_folder2">folder2</string>
<string name="png_file_name">ic_launcher.png</string>
</resources>
|
import android.content.Context;
import android.os.Build;
import android.util.Log;
import java.io.File;
public class clsCommon {
//0:存在する
//1:存在しない
public int existsFolderOrFile(Context context,String sFileName) {
int iResult = 0;
//ファイルオブジェクトを生成
File file = null;
try
{
file = context.getFileStreamPath(sFileName);
//ファイル存在チェック
if (file.exists() != true) iResult = 1;
}
catch (Exception e)
{
Log.d("error",e.getMessage().toString() );
return 1;
}
finally
{
file=null;
}
return iResult;
}
//strings.xmlから指定した文字列を取得します。
//context:Context
//iResourceId:strings.xmlに定義しているリソースId
public String getStringXmlData(Context context, int iResourceId)
{
//strings.xmlからリソースIDを指定して文字列を取得します。
return context.getString(iResourceId);
}
public String getPath(Context context, String sFileName)
{
//ファイルのパス
String sPath = context.getFilesDir().getAbsolutePath() + "/" + sFileName;
//ex.)
//sPath: /data/data/test.example.com.test23/files/ic_launcher.png
return sPath;
}
public String getAbsolutePath(Context context)
{
return context.getFilesDir().getAbsolutePath();
}
public void getCurrentVersion()
{
//バージョンの数字を取得する方法
/*
double dVersion=0.0f;
dVersion= Build.VERSION_CODES.HONEYCOMB;
Log.d("HONEYCOMB Version", String.valueOf(dVersion));
dVersion= Build.VERSION_CODES.KITKAT;
Log.d("KITKAT Version", String.valueOf(dVersion));
dVersion= Build.VERSION_CODES.LOLLIPOP;
Log.d("LOLLIPOP Version", String.valueOf(dVersion));
*/
int iVersion=0;
iVersion= Build.VERSION.SDK_INT;
Log.d("Build.VERSION.SDK_INT", String.valueOf(iVersion));
}
}
|
|
|