2011-02-08 8 views
2

カメラから写真を撮ってビットマップとして取得するアプリを書く。私はこのファイルをディスクに書き込んで写真を電子メールで送信しようとしますが、Gmailのアプリで添付ファイルがありますが、電子メールを受信して​​も添付ファイルはありません。ここに私が働くことを試みている2つの適切な方法があります。電子メールに画像を添付する

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 
    thumbnail = (Bitmap) data.getExtras().get("data"); 
    ImageView image = (ImageView) findViewById(R.id.photoResultView); 
    image.setImageBitmap(thumbnail); 


    try { 
      pic = new File("pic"); 

     FileOutputStream out = new FileOutputStream(pic); 
     thumbnail.compress(CompressFormat.PNG, 100, out); 
     out.flush(); 
     out.close(); 

    } 
    catch (java.io.FileNotFoundException e) { 
     // that's OK, we probably haven't created it yet 
    } 
    catch (Throwable t) { 
     Toast 
      .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG) 
      .show(); 

    } 
    sendPictureButton.setVisibility(Button.VISIBLE); 
    } 
} 
private OnClickListener sendPictureButtonListener = new OnClickListener() { 

    @Override 
    public void onClick(View arg0){ 

     Intent i = new Intent(Intent.ACTION_SEND); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT,"On The Job"); 
     i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic)); 

     i.setType("image/png"); 
     startActivity(Intent.createChooser(i,"Share you on the jobing")); 

    } 
    }; 

答えて

1

最初にSDカードにアクセスし、それを見つけて書き込み、ファイルのURIを取得する必要があります。ここで

は、機能コードです:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 
    thumbnail = (Bitmap) data.getExtras().get("data"); 
    ImageView image = (ImageView) findViewById(R.id.photoResultView); 
    image.setImageBitmap(thumbnail); 


     try { 
      File root = Environment.getExternalStorageDirectory(); 
      if (root.canWrite()){ 
       pic = new File(root, "pic.png"); 
       FileOutputStream out = new FileOutputStream(pic); 
       thumbnail.compress(CompressFormat.PNG, 100, out); 
       out.flush(); 
       out.close(); 
      } 
     } catch (IOException e) { 
      Log.e("BROKEN", "Could not write file " + e.getMessage()); 
     } 
    sendPictureButton.setVisibility(Button.VISIBLE); 
    } 
} 


private OnClickListener takePictureButtonListener = new OnClickListener() { 

    @Override 
    public void onClick(View arg0){ 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

    } 
    }; 

    private OnClickListener sendPictureButtonListener = new OnClickListener() { 

    @Override 
    public void onClick(View arg0){ 

     Intent i = new Intent(Intent.ACTION_SEND); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT,"On The Job"); 
     //Log.d("[email protected][email protected]#!#[email protected]##!", Uri.fromFile(pic).toString() + " " + pic.exists()); 
     i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic)); 

     i.setType("image/png"); 
     startActivity(Intent.createChooser(i,"Share you on the jobing")); 
    } 
    }; 
1

私はこの問題を抱えていた同僚を持っていた、それはあなたがそれを送信しようとこのと一緒にあなたがアクセスするために必要な許可のを持っていることを確認しているとき、ファイルが存在しないことを確認、ファイルを作成することはありませんディスクドライブ。

+0

おかげで、正しい方向に私を導きました – NickTFried

関連する問題