繁体   English   中英

用拇指保存图像并在imageView android上显示

[英]Saving an image with its thumb and showing on imageView android

我正在尝试保存从相机拍摄的图像,然后将其存储在sdCard上,同时将其拇指也显示在imageView上。 但是它在Null指针处给出错误

 imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false);

怎么了?

    {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            try 
            {
              if (title.getText().toString().equals(""))
              {
                displayAlert("Please Input Title First","Error!");
              }
              else
              {

                Integer val = myMisc.miscId ;
                String fileName = "image" + "_" + title.getText().toString()+"_" + val.toString();
                photo = this.createFile(fileName, ".jpg");
                myMisc.filename = photo.getAbsolutePath();
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                startActivityForResult(intent, RESULT_CAMERA_SELECT);
              }
            }
            catch(Exception e)
            {
                Log.v("Error", "Can't create file to take picture!");
                displayAlert("Can't create file to take picture!","SDCard Error!");
            }
        }




public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data) 
    {
       if (resultCode == Activity.RESULT_OK) 
       {
           if (requestCode == RESULT_CAMERA_SELECT)
           {
               try 
               {
                   saveImage();
               }
               catch (IOException e) 
               {
                   e.printStackTrace();
               }
           }



public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

    public void saveImage() throws IOException
    {
      try 
        {       
           FileInputStream is2 = new FileInputStream(photo);
           final BitmapFactory.Options options = new BitmapFactory.Options();
           options.inJustDecodeBounds = true;
           Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options);
           options.inSampleSize = calculateInSampleSize(options, 40, 40);
           options.inJustDecodeBounds = false;
           imageBitmap = BitmapFactory.decodeStream(is2 ,null, options);
           imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false);
           Integer val = myMisc.miscId;
           String fileName = ".thumbImage" + "_" + title.getText().toString()+ "_" + val.toString();  
           photo = this.createFile(fileName, ".jpg");
           myMisc.thumbFileName = photo.getAbsolutePath();
        try {
           FileOutputStream out = new FileOutputStream(photo);
           imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            } catch (Exception e) {
           e.printStackTrace();
            }
            is2.close();
            Uri uri = Uri.fromFile(photo);
            photo = null;
            imageBitmap = null;
            imageView.setImageURI(uri);
        }catch(Exception e)
        {
            displayAlert("Can't create file to take picture!","SD Card Error");
        }
    }

这可能是因为decodeStream方法。

Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options);

您在询问Rectangle时解析null,当您尝试创建缩放位图时,它无效。

我发现我的错误Bitmap imageBitmap = BitmapFactory.decodeStream(is2, null, options); 解码两次

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM