csh_34 2013-06-03 03:44 采纳率: 0%
浏览 2264
已采纳

保存一个图像连同它的thuml然后显示在imageView中

我想保存一个从相机拍摄的照片,然后把照片和 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");
        }
    }
  • 写回答

2条回答 默认 最新

  • luhuajcdd 2013-06-03 09:27
    关注

    public static Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
    
        // Calculate inSampleSize
        options.inSampleSize = 2;
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
    
        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        if(bm == null){
            return  null;
        }
        ByteArrayOutputStream baos = null ;
        try{
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        }finally{
            try {
                if(baos != null)
                    baos.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bm ;
    
    }
    

    这样可以从文件中得到图片。

    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
                File f = new File(mCurrentPhotoPath);
                Uri contentUri = Uri.fromFile(f);
                mediaScanIntent.setData(contentUri);
                this.sendBroadcast(mediaScanIntent);
    

    上面的code可以把图片保存到图片库中

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题