当我从图库中选择图片并将其集成到Gmail时,应用程序崩溃

时间:2014-02-18 22:44:40

标签: android email android-intent attachment email-attachments

我正在尝试创建一个应用程序,让用户从库中选择一个图像或者换一个新图像,然后通过电子邮件发送它。拍摄照片的功能完美无缺(我可以拍摄然后通过电子邮件发送)。但是当我尝试选择一张图片时,我的应用程序崩溃了。这是我的Java文件:

 public class Request extends Activity {
 Button send;
 Bitmap thumbnail;
 File pic;
 protected static final int CAMERA_PIC_REQUEST = 0;
 private static final int SELECT_PICTURE = 1;
 private String selectedImagePath;

 @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.requestscreen);
  send=(Button) findViewById(R.id.sendBtn);




  Button camera = (Button) findViewById(R.id.camBtn); 
  camera.setOnClickListener(new View.OnClickListener() {

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

  }
   });

  Button galbtn = (Button) findViewById(R.id.galBtn);
 galbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,
            "Select Picture"), SELECT_PICTURE);
}
 });

send.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0){

    Intent i = new Intent(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@test.com"});
    i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
    //Log.d("URI@!@#!#!@##!", 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());
 }   
 }

   if (requestCode == SELECT_PICTURE){
   Uri selectedImageUri = data.getData();
   selectedImagePath = getPath(selectedImageUri);
   TextView uritv = null;
uritv.setText(selectedImagePath.toString());
}
 }

public String getPath(Uri uri) {
 String[] projection = { MediaStore.Images.Media.DATA };
 Cursor cursor = managedQuery(uri, projection, null, null, null);
 int column_index = cursor
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 cursor.moveToFirst();
  return cursor.getString(column_index);
 }
}

我不知道我的代码需要更改什么,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

似乎对我很清楚

 TextView uritv = null;
 uritv.setText(selectedImagePath.toString());

换句话说,您正在尝试在null对象上使用方法。

我的猜测是你没有实现真正的TextView,就像

TextView uritv  = (TextView) findViewById(R.id.uritv);

当然,这取决于你的文本视图的真实命名