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

标签 java android eclipse

我正在尝试保存从相机拍摄的图像,然后将其连同其 Thumb 一起存储在 sdCard 上,并在 imageView 上显示此拇指。 但是它在空指针处给出错误

 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 方法。

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

您在请求矩形时解析了 null,而当您尝试创建缩放位图时它无效。

关于java - 用拇指保存图像并在 imageView android 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379084/

相关文章:

java - 像在 Android 的 Facebook 应用程序中一样创建登录布局

c# - 如何从具有不同内容的单个 Android 应用程序创建多个应用程序

android - 向eclipse添加jar文档

eclipse - 你如何让 eclipse 使用现有的 svn 工作副本?

eclipse - 如何在不提交的情况下使用 git 同步代码?

java - jackson xml反序列化内联数组

java - Mockito.extension 未找到 JUnit 5 throw 方法

java - Groovy StreamingTemplateEngine 使用 withCredentials 函数给出错误

java - 单元测试中是否使用了 Spring Framework?

android - 我们如何将动画/简单图像添加为视频的顶层并将其导出为 Android 中的单个视频?