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条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码