0

我已经编写了捕获照片并将捕获的图像上传到 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();
                        }
4

1 回答 1

0

try to decode as below

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;
    }
于 2012-11-27T05:47:45.600 回答