Android Studio にてOneDrive上に保存しているtxtファイルの中身の書き換えとフォルダ名の変更方法について教えていただきたく思います。
具体的には、
/////////////////////////////////////////////////////////////
アプリ起動
↓
ボタンを押す
↓
ボタンが押されたら
OneDrive上にもともと置いてあるtest1.txt(中に特に記述はない。
の中身を1とだけ書き換え、上書き保存。
test1.txtの名前をtest2.txtへと書き換える。
/////////////////////////////////////////////////////////////
といったようなアプリを作成したいです。
/////////////////////////////////////////////////////////////
2016/11/22追記
Android 向けの OneDrive ピッカーおよぴセーバー
↑上記のサイトの
"onedrive-picker-android"
に入っている
"SaverSample"内の"SaverMain.java"
を利用すれば、アプリ上からテキストファイルの内容を指定し、保存することができる。というのはわかったのですが、どの文章でテキストフォルダ内の内容を決めているのかがわかりません。
SaverMain.javaソースコード
java
1コード 2 3// ------------------------------------------------------------------------------ 4// Copyright (c) 2014 Microsoft Corporation 5// 6// Permission is hereby granted, free of charge, to any person obtaining a copy 7// of this software and associated documentation files (the "Software"), to deal 8// in the Software without restriction, including without limitation the rights 9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10// copies of the Software, and to permit persons to whom the Software is 11// furnished to do so, subject to the following conditions: 12// 13// The above copyright notice and this permission notice shall be included in 14// all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22// THE SOFTWARE. 23// ------------------------------------------------------------------------------ 24 25package com.example.onedrivesdk.saversample; 26 27import java.io.*; 28 29import android.app.Activity; 30import android.content.Intent; 31import android.net.Uri; 32import android.os.*; 33import android.view.View; 34import android.view.View.OnClickListener; 35import android.widget.*; 36 37import com.microsoft.onedrivesdk.saver.*; 38 39/** 40 * Activity that shows how the OneDrive SDK can be used for file saving 41 * 42 * @author pnied 43 */ 44public class SaverMain extends Activity { 45 46 /** 47 * The default file size 48 */ 49 private static final int DEFAULT_FILE_SIZE_KB = 100; 50 51 /** 52 * Registered Application id for OneDrive {@see http://go.microsoft.com/fwlink/p/?LinkId=193157} 53 */ 54 private static final String ONEDRIVE_APP_ID = "48122D4E"; 55 56 /** 57 * The onClickListener that will start the OneDrive Picker 58 */ 59 private final OnClickListener mStartPickingListener = new OnClickListener() { 60 @Override 61 public void onClick(final View v) { 62 final Activity activity = (Activity) v.getContext(); 63 activity.findViewById(R.id.result_table).setVisibility(View.INVISIBLE); 64 65 final String filename = ((EditText)activity.findViewById(R.id.file_name_edit_text)) 66 .getText().toString(); 67 final String fileSizeString = ((EditText)activity.findViewById(R.id.file_size_edit_text)) 68 .getText().toString(); 69 int size; 70 try { 71 size = Integer.parseInt(fileSizeString); 72 } catch (final NumberFormatException nfe) { 73 size = DEFAULT_FILE_SIZE_KB; 74 } 75 76 // Create a file 77 final File f = createExternalSdCardFile(filename, size); 78 79 // Start the saver 80 mSaver.startSaving(activity, filename, Uri.parse("file://" + f.getAbsolutePath())); 81 } 82 }; 83 84 /** 85 * The OneDrive saver instance used by this activity 86 */ 87 private ISaver mSaver; 88 89 @Override 90 protected void onCreate(final Bundle savedInstanceState) { 91 super.onCreate(savedInstanceState); 92 setContentView(R.layout.activity_saver_main); 93 94 // Create the picker instance 95 mSaver = Saver.createSaver(ONEDRIVE_APP_ID); 96 97 // Add the start saving listener 98 findViewById(R.id.startSaverButton).setOnClickListener(mStartPickingListener); 99 } 100 101 @Override 102 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 103 // Check that we were able to save the file on OneDrive 104 final TextView overallResult = (TextView) findViewById(R.id.overall_result); 105 final TextView errorResult = (TextView) findViewById(R.id.error_type_result); 106 final TextView debugErrorResult = (TextView) findViewById(R.id.debug_error_result); 107 108 try { 109 mSaver.handleSave(requestCode, resultCode, data); 110 overallResult.setText(getString(R.string.overall_result_success)); 111 errorResult.setText(getString(R.string.error_message_none)); 112 debugErrorResult.setText(getString(R.string.error_message_none)); 113 } catch (final SaverException e) { 114 overallResult.setText(getString(R.string.overall_result_failure)); 115 errorResult.setText(e.getErrorType().toString()); 116 debugErrorResult.setText(e.getDebugErrorInfo()); 117 } 118 findViewById(R.id.result_table).setVisibility(View.VISIBLE); 119 } 120 121 /** 122 * Creates an file on the SDCard 123 * @param filename The name of the file to create 124 * @param size The size in KB to make the file 125 * @return The {@link File} object that was created 126 */ 127 private File createExternalSdCardFile(final String filename, final int size) { 128 final int bufferSize = 1024; 129 final int alphabetRange = 'z' - 'a'; 130 File file = null; 131 try { 132 file = new File(Environment.getExternalStorageDirectory(), filename); 133 final FileOutputStream fos = new FileOutputStream(file); 134 135 // Create a 1 kb size buffer to use in writing the temp file 136 byte[] buffer = new byte[bufferSize]; 137 for (int i = 0; i < buffer.length; i++) { 138 buffer[i] = (byte)('a' + i % alphabetRange); 139 } 140 141 // Write out the file, 1 kb at a time 142 for (int i = 0; i < size; i++) { 143 fos.write(buffer, 0, buffer.length); 144 } 145 146 fos.close(); 147 } catch (final IOException e) { 148 Toast.makeText(this, "Error when creating the file: " + e.getMessage(), Toast.LENGTH_LONG).show(); 149 } 150 return file; 151 } 152} 153 154
//////////////////////////////////////////////////////////////////////////////////////////////////////////```
2016/11/22追記2
Y.H.さんへの返信
このサンプルプログラムを実行した場合、'a'から'z'まで指定キロバイトまで延々と出力されたテキストファイルができるわけなのですが、例えばテキストファイルの中身を1とだけ入れたい場合は、
java
1コード 2 byte[] buffer = new byte[bufferSize]; 3 for (int i = 0; i < buffer.length; i++) { 4 buffer[i] = (byte)('a' + i % alphabetRange); 5 }
のbuffer[i] = (byte)(このぶぶんを変更);
すればよいのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/22 06:37
2016/11/22 07:13