Java 选择文件时出现空指针异常
我正在编写一个联系人表单,其中我正在创建一个带有按钮的文件选择器,但问题是,如果文件大小很大,那么它将不幸停止,如果没有文件资源管理器,那么它将给出空指针异常。 这是意向书Java 选择文件时出现空指针异常,java,android-intent,nullpointerexception,Java,Android Intent,Nullpointerexception,我正在编写一个联系人表单,其中我正在创建一个带有按钮的文件选择器,但问题是,如果文件大小很大,那么它将不幸停止,如果没有文件资源管理器,那么它将给出空指针异常。 这是意向书 Intent intent= new Intent(Intent.ACTION_PICK); intent.setAction(Intent.ACTION_GET_CONTENT);// its optional intent.setType("image/*");
Intent intent= new Intent(Intent.ACTION_PICK);
intent.setAction(Intent.ACTION_GET_CONTENT);// its optional
intent.setType("image/*");
final int SELECT_IMAGE = 1;
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_IMAGE);
and here is the code of on activity result
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == 1 && resultCode == RESULT_OK ) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
//this code converts image in base64 and the string is to be sent along post method:
yourSelectedImage = BitmapFactory.decodeFile(filePath);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, resultCode, Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
添加stacktrace并标记引发异常的行。5924-5924/darteweb.cqc E/AndroidRuntime﹕ 致命异常:main java.lang.OutOfMemoryError:(Heap Size=32803KB,Allocated=29166KB)使用编辑功能添加完整的堆栈跟踪。