我已经编写了捕获照片并将捕获的图像上传到 Facebook 的代码。它适用于所有设备,但是当我尝试在三星 Galaxy s3 上运行应用程序时,应用程序崩溃并出现内存不足错误,当我压缩图像时它显示空指针异常。
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
this.file = new File(
Environment
.getExternalStorageDirectory(),
"tmp_"
+ String.valueOf(System
.currentTimeMillis())
+ ".jpg");
ImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT,
ImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent,
PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
::onActivityResult
uploadBitmap= decodeFile(MainActivity.this.file);
int width = uploadBitmap.getWidth()/3;
int height = uploadBitmap.getHeight()/3;
// uploadBitmap=Bitmap.createScaledBitmap(uploadBitmap, 50, 50, false);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
uploadBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
// Bitmap bitmap = BitmapFactory.decodeFile(path);
if (photoUrl != null) {
Bundle params = new Bundle();
params.putByteArray("photo", byteArray);
params.putString("caption",
MainActivity.locationData+" lat: "+MainActivity.latitude+" long:"+MainActivity.longitude);
String place ="\"location\""+": {"+
"\"latitude\""+":"+"\"12.9833\""+","+
"\"longitude\""+":"+"\"77.5833\""+
"}";
params.putString("place", placeid);
Utility.mAsyncRunner.request("me/photos", params, "POST",
new PhotoUploadListener(), null);
} else {
Toast.makeText(getApplicationContext(),
"Error capturing image",
Toast.LENGTH_SHORT).show();
}