从Bitmap压缩获取NullPointerException

时间:2015-11-03 18:33:24

标签: java bitmap compression

我看到了一些东西,所以我的代码非常类似于你可以轻松找到的那个:) 但是我无法让它工作得很糟糕,得到以下例外:

  

引起:java.lang.NullPointerException:尝试调用虚方法' boolean   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'在null对象引用上               在monsita.h24shops.MapsActivity.upload(MapsActivity.java:301)

我从按钮调用takePic()方法 我可以拍照,我在我的画廊中看到它,在的正确文件夹中

Log.e("path", "----------------" + picturePath);

给了我。 这些是我使用的功能:

int TAKE_PHOTO_CODE = 0;
public static int count = 0;
public static String ba1;
String picturePath;

public void takePic(){

    SharedPreferences sharedpreferences = getSharedPreferences("label", 0);

    count = sharedpreferences.getInt("lastpic", 0);

    //here,we are making a folder named picFolder to store pics taken by the camera using this application
    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
    File newdir = new File(dir);
    newdir.mkdirs();

    // here,counter will be incremented each time, based on getInt by sharedpref
    count++;
    String filePath = dir+count+".jpg";
    File newfile = new File(filePath);

    try {
        newfile.createNewFile();
    } catch (IOException e) {}

    Uri outputFileUri = Uri.fromFile(newfile);

    picturePath = filePath;
    Toast.makeText(this,picturePath,Toast.LENGTH_SHORT).show();

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}







@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        upload();
    }
}




 private void upload() {

    Log.e("path", "----------------" + picturePath);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, options);
    //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inSampleSize = Utils.calculateInSampleSize(options, 150, 150);

    Bitmap bm = BitmapFactory.decodeFile(picturePath,options);  //options added
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    //bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);     //90,50... is pic quality from 1 to 100
    bm.compress(Bitmap.CompressFormat.PNG, 30, bao);

    byte[] ba = bao.toByteArray();
    //ba1 = Base64.encodeBytes(ba);    //era encodeBytes
    ba1 = new String(Base64.encodeBase64(ba));

    Log.e("base64", "-----" + ba1);

    //android:largeHeap="true" added in Manifest to manage big bitmap and avoid OOM error

    // Upload image to server
    new uploadToServer(MapsActivity.this,mapManager.getclickedMarkerID()).execute();
}

在清单中我有:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="monsita.h24shops.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.camera2" android:required="true" />
<uses-permission android:name="android.permission.camera" />

0 个答案:

没有答案