0

SD カードに保存した画像を含む MMS を送信しようとしていますが、メッセージング サービスを開こうとすると、「申し訳ありませんが、この画像をメッセージに追加できません」というメッセージが表示され、ファイルが添付されません。ファイルを uri に解析してみたり、ファイルを MMS インテントに直接渡したりしてみました。何が欠けているのかわからず、ファイル ビューアーでファイルを確認できるので、ファイルが保存されていることは確かです。イメージをスキャンしてメディアストアで使用できるようにする必要がありますか (イメージ ギャラリーには入れたくない)、ファイルを渡す前にまずファイルを開く必要がありますか? 私が何をすべきかについての少しの方向性をいただければ幸いです。

私のファイル

private static final String FILENAME = "data.pic";
File dataFile = new File(Environment.getExternalStorageDirectory(), FILENAME);

画像を保存する

// Selected image id
int position = data.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
Resources res = getResources();
Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
try {
    File file = new File(dataFile, FILENAME);
    file.mkdirs();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bitmapdata);
    fos.close();
 }catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }

}

取り付けたい場所と現在の試み

// Create a new MMS intent
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.putExtra("sms_body", "I sent a pic to you!");
mmsIntent.putExtra("address", txt1);
mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(FILENAME)));
mmsIntent.setType("image/png");
startActivity(mmsIntent);
}};
4

1 に答える 1

2

Aaah 申し訳ありませんが、できません。

問題:

new File(FILENAME)存在しません。

これらの 3 つの異なる Files コード行を見てください。

1. File dataFile = new File(Environment.getExternalStorageDirectory(), FILENAME);

2. File file = new File(dataFile, FILENAME);

3. Uri.fromFile(new File(FILENAME))

すべてに違いがあります。

解決:

コードを変更する

try {
     File file = new File(dataFile, FILENAME); 
     file.mkdirs(); // You are making a directory here
     FileOutputStream fos = new FileOutputStream(file); // set Outputstream for directory which is wrong
     fos.write(bitmapdata);
     fos.close();
    }catch (FileNotFoundException e) {
     e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

try {
      FileOutputStream fos = new FileOutputStream(dataFile);
      fos.write(bitmapdata);
      fos.close();
     }catch (FileNotFoundException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
    }

そして、MMS を送信するためのメイン コード行、

if(dataFile.exists())
{
 Intent mmsIntent = new Intent(Intent.ACTION_SEND);
     mmsIntent.putExtra("sms_body", "I sent a pic to you!");
     mmsIntent.putExtra("address", txt1);
     mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
     mmsIntent.setType("image/png");
     startActivity(mmsIntent);
}

dataFile Fileすべてのコードに対して1 つの参照のみを使用してください。

于 2012-11-24T16:07:39.023 に答える