当我选择从画廊的照片,并将其整合到Gmail应用程序崩溃当我、画廊、应用程序、照片

2023-09-12 08:22:24 作者:凉城无爱≈

我试图创建一个应用程序,让用户选择从画廊的图片或采取一个新的,然后通过电子邮件发送。该拍摄照片的功能完美地工作(我可以把它,然后通过电子邮件发送)。但是,当我尽量选择一张照片,我的应用程序崩溃。这是我的Java文件:

I'm trying to create an application that lets the user choose an image from the gallery or take a new one, and then send it via email. The function that takes the picture works perfectly (I can take it and then email it). But when I try to choose a picture, my application crashes. Here is my Java file:

 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);
 }
}

我不知道我需要在我的code改变,谁能帮助我?

I don't know what I need to change in my code, can anyone help me?

推荐答案

似乎很清楚,以我

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

在换句话说,你要在一个空物体使用的方法。

In other words, you're trying to use a method on a null object.

我的猜测是你不实例化真正的TextView这会是这样

My guess is you're not instancing the real TextView which would be something like

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

当然这取决于你的TextView的真正命名

which, of course, depends on the real nomenclature of your textview

 
精彩推荐
图片推荐