我已经编写了捕获照片的代码并将捕获的图像上传到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();
}
答案 0 :(得分:0)
尝试解码如下
public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}