保存Layer Drawable时面临错误

时间:2016-08-12 17:22:10

标签: android-studio bitmap android-bitmap bitmapimage layerdrawable

我是初学者,我正在尝试创建像retrica或其他类似应用的应用。但是我面临一些问题,例如从imageview.i'm保存图像,使用layerdrawable来实现框架,clip_arts和过滤器等。我收到错误

08-12 22:01:55.812 9469-9469 / com.zaibatsu.usman.camerashop D / My Error :: java.lang.NullPointerException:尝试调用虚方法'void android.graphics.drawable.LayerDrawable.setBounds (int,int,int,int)'在空对象引用

实际上我将layerdrwable转换为位图图像所以,我想将编辑后的图像保存到设备内存中。 这是我的代码......

公共类显示扩展活动{

static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView CapturedImageView;
LayerDrawable layerDrawable;
Uri uri; String ImagePath;
Bitmap bitMapImage;
OutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

    Button CaptureButton= (Button)findViewById(R.id.btnCapture);
    CapturedImageView = (ImageView)findViewById(R.id.CapturedImageView);

    if (!hasCamera())
    {
        CaptureButton.setEnabled(false);
    }
}

private boolean hasCamera()
{
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void onButtonClick(View view)
{
    if (view.getId()== R.id.btnCapture)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
    if (view.getId()==R.id.Frame1)
    {
        Drawable[] layers = new Drawable[2];
        layers[0] = CapturedImageView.getDrawable();
        layers[1] = getResources().getDrawable(R.drawable.f1);
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        CapturedImageView.setImageDrawable(layerDrawable);
    }
    if (view.getId()==R.id.btnSave){

        try {

            BitmapDrawable bDrawable  = (BitmapDrawable) getSingleDrawable(layerDrawable);
            Toast.makeText(getApplicationContext(),"No E", Toast.LENGTH_LONG).show();
            bitMapImage = bDrawable.getBitmap();
            MediaStore.Images.Media.insertImage(getContentResolver(),bitMapImage,"title","Description");
        }
        catch (Exception e){
            Toast.makeText(Display.this, "My Error : "+e, Toast.LENGTH_LONG).show();
            Log.d("My Error : ", ""+e);
        }
    }
}
public Drawable getSingleDrawable(LayerDrawable layerDrawabl){
    int resourceBitmapHeight = 136, resourceBitmapWidth = 153;
    float widthInInches = 0.9f;
    int widthInPixels = (int)(widthInInches * getResources().getDisplayMetrics().densityDpi);
    int heightInPixels = (widthInPixels * resourceBitmapHeight / resourceBitmapWidth);
    int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
    layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);
    Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    layerDrawabl.setBounds(0, 0, widthInPixels, heightInPixels);
    layerDrawabl.draw(canvas);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
    bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
    return bitmapDrawable;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK);
    {
        Bundle extras = data.getExtras();
        Bitmap CapturePhoto = (Bitmap) extras.get("data");
        CapturedImageView.setImageBitmap(CapturePhoto);
    }
}

}

0 个答案:

没有答案