2014-09-26 3 views
1

두 가지 선택이있는 프로젝트에서 작업하고 있는데 사진을 클릭하면 동일한 image이 첨부되어 전송 될 수 있습니다. 두 번째는 gallery에서 image을 선택하고 선택한 이미지가 mail의 첨부 파일로 전송됩니다. 나중에 부분을 수행 할 수 있지만 camera을 사용하여 이미지를 첨부 한 후에 이미지를 첨부하는 데 문제가 있습니다.이미지를 첨부하여 안드로이드 카메라를 사용합니다

public class MainActivity extends Activity implements OnClickListener { 

Button select; 
ImageView photo; 
EditText et_subject, et_message; 
TextView tv_attach; 
String subject, message; 


private static final int PICK_IMAGE = 100; 
Uri URI = null; 
Uri URI1= null; 
int columnindex; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    select = (Button)findViewById(R.id.button1); 
    photo = (ImageView)findViewById(R.id.imageView1); 

    et_subject = (EditText)findViewById(R.id.editText1); 
    et_message = (EditText)findViewById(R.id.editText2); 
    tv_attach = (TextView)findViewById(R.id.textView1); 

    photo.setOnClickListener(this);  
    select.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
    case R.id.imageView1: 
     selectImage(); 
     break; 

    case R.id.button1: 
     subject = et_subject.getText().toString(); 
     message = et_message.getText().toString(); 

     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     emailIntent.setType("plain/text"); 
     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
     if (URI != null || URI1 != null) 
      emailIntent.putExtra(Intent.EXTRA_STREAM, URI); 

     startActivity(emailIntent); 
     break; 

     default: 
      break; 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds options to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private void selectImage() { 

    final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(options, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (options[item].equals("Take Photo")) 
      { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
       startActivityForResult(intent, 1); 
      } 
      else if (options[item].equals("Choose from Gallery")) 
      { 
       Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, 2); 

      } 
      else if (options[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      File f = new File(Environment.getExternalStorageDirectory().toString()); 
      for (File temp : f.listFiles()) { 
       if (temp.getName().equals("temp.jpg")) { 
        f = temp; 
        break; 
       } 
      } 
      try { 
       Bitmap bitmap; 
       BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

       bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
         bitmapOptions); 

       photo.setImageBitmap(bitmap); 

       String path = android.os.Environment 
         .getExternalStorageDirectory() 
         + File.separator 
         + "Phoenix" + File.separator + "default"; 
       URI1 = Uri.parse("file://" + path); 
       //f.delete(); 
       OutputStream outFile = null; 
       File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 
       try { 
        outFile = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
        outFile.flush(); 
        outFile.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else if (requestCode == 2) { 

      Uri selectedImage = data.getData(); 
      String[] filePath = { MediaStore.Images.Media.DATA }; 
      Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(filePath[0]); 
      String picturePath = c.getString(columnIndex); 
      URI = Uri.parse("file://" + picturePath); 
      c.close(); 
      Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 
      Log.w("path of image from gallery......******************.........", picturePath+""); 
      photo.setImageBitmap(thumbnail); 
     } 
    } 
} 
} 

나는 무언가를 시도했지만 틀렸어.

제발 내가 할 수있는 일을 도와 주시겠습니까? 아무도 내가 사용한 코드를 변경할 수 있습니까?

답변

0

나는 그것을 시도하고 그것은 나를 위해 일했다, 그것이 도움이되기를 바랍니다!

mail_btn.setOnClickListener(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")); 
    } 
    }); 


} 

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.imageView1); 
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()); 
    } 

} 
} 
+0

내가 제공 한 코드를 변경할 수 있습니까? 내 코드를 올바른 코드로 편집하는 것이 좋습니다. 그게 끝날 수 있습니까? –

관련 문제