问题
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);
}
}
I don't know what I need to change in my code, can anyone help me?
回答1:
Seems clear to me
TextView uritv = null;
uritv.setText(selectedImagePath.toString());
In other words, you're trying to use a method on a null object.
My guess is you're not instancing the real TextView which would be something like
TextView uritv = (TextView) findViewById(R.id.uritv);
which, of course, depends on the real nomenclature of your textview
来源:https://stackoverflow.com/questions/21867104/application-crashes-when-i-choose-a-picture-from-gallery-and-integrate-it-into-g