プロが教える店舗&オフィスのセキュリティ対策術

OS は Windows 7 64bit です。

Eclipse を使って、Android のメールソフトを作っています。
メールの添付ファイルを取得しようとして、
いろいろやっています。

os = new BufferedOutputStream(new FileOutputStream(file));

のような部分で失敗します。

エラーメッセージの内容には、

Read-only file system

とあります。
書き込み可能にする方法はありますか?

よろしくお願いします。

A 回答 (2件)

Androidのプログラムは組んだことありませんけど、もしかしてファイル入出力のためのオープン処理はopenFileInput()

,openFileOutput()を使わないといけないのではないですか?(new FileOutputStream(file)を直接使ってはいけない)
    • good
    • 0
この回答へのお礼

ありがとうございました、

理由はよく分かりませんが、
次のようにしたら、添付ファイルが取り出せました。
base64 デコードもしてくれています。

if (attachment) {
String disp = part.getDisposition();
// 添付ファイルの場合
if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
String filename = part.getFileName();
if (filename != null) {
filename = MimeUtility.decodeText(filename);
} else {
filename = "添付ファイル" + html;
}

String newName = filename;
File f = new File(filename);
// find a file that does not yet exist
for (int i = 1; f.exists(); i++) {
newName = filename + i;
f = new File(newName);
}
try {
FileOutputStream writer=openFileOutput(newName, MODE_PRIVATE);
InputStream in2 = new BufferedInputStream(part.getInputStream());

int b;
while ((b = in2.read()) != -1) writer.write(b);
writer.flush();

if (writer != null)
writer.close();
if (in2 != null)
in2.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

お礼日時:2014/12/31 15:33

「書き込み可能な場所にファイルを作ってください」としかいいようがないような。

この回答への補足

ありがとうございます。

PCでのフォルダやファイルの属性なら見て変更もできますが、

Eclipse での Android アプリ開発では、
そもそも、PCのどこにファイルがあるのか、フォルダの属性の
変更方法など、全く分かりません。

DDMS を使って、見てみると、
ほかの、プロジェクトでファイルの作成が可能なものの場合は
files という フォルダがあるのですが、
この、作成中のプロジェクトにはありません。

また、DDMSで files のフォルダを作っても、
書き込みはできません。

書き込みできる場所の作り方、見つけ方を教えていただければ幸いです。
よろしくお願いします。

補足日時:2014/12/31 07:21
    • good
    • 0
この回答へのお礼

ご指導、ありがとうございます。

ほかのサンプルと比較しながらいろいろ試しました。

public static void dumpPart(Part part) throws Exception {
String html = "";
boolean attachment = false;


if (part instanceof Message) {

-- 省略---

// 内容
String scont = "内容:\n" + message.getContent();
}

if (part.isMimeType("text/plain")) { // テキストの場合
String scont = "内容:\n" + part.getContent();
} else if (part.isMimeType("multipart/*")) { // マルチパートの場合
Multipart mp = (Multipart) part.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) { // メッセージの場合
dumpPart((Part) part.getContent());
} else if (part.isMimeType("text/html")) { // HTMLの場合
html = ".html";
attachment = true;
} else { // その他の場合
attachment = true;
}

if (attachment) {
String disp = part.getDisposition();
// 添付ファイルの場合
if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
String filename = part.getFileName();
if (filename != null) {
filename = MimeUtility.decodeText(filename);
} else {
filename = "添付ファイル" + html;
}
///////////////////////////////////////////
/*
try{

FileOutputStream fos=openFileOutput(filename, MODE_PRIVATE);
String text="openFileOutputで書き込み";
fos.write(text.getBytes());
fos.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}

try{
FileInputStream fis=openFileInput(filename);
EditText editText1=(EditText)findViewById(R.id.editText1);
byte buffer[]=new byte[100];
fis.read(buffer);
editText1.setText(new String(buffer).trim());
fis.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}
*/
File f = new File(filename);

try {
OutputStream writer = new BufferedOutputStream(new FileOutputStream(f));
InputStream in2 = new BufferedInputStream(part.getInputStream());

int b;
while ((b = in2.read()) != -1) writer.write(b);
writer.flush();
writer.close();

if (writer != null)
writer.close();
if (in2 != null)
in2.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

で、
public static void dumpPart(Part part) throws Exception {
を、
public void dumpPart(Part part) throws Exception {
に変更すると、
コメントアウトした部分、
/*
try{

FileOutputStream fos=openFileOutput(filename, MODE_PRIVATE);
String text="openFileOutputで書き込み";
fos.write(text.getBytes());
fos.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}

try{
FileInputStream fis=openFileInput(filename);
EditText editText1=(EditText)findViewById(R.id.editText1);
byte buffer[]=new byte[100];
fis.read(buffer);
editText1.setText(new String(buffer).trim());
fis.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}
*/
は、動いてファイルが作成されます。

しかし、自動的に、base64 デコードをしてもらうつもりで、欲張ると、
OutputStream writer = new BufferedOutputStream(new FileOutputStream(f));
の部分で、エラーになってしまいます。

原因がわかりません。
何かヒントがありましたらよろしくお願いします。

お礼日時:2014/12/31 11:49

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!