【问题标题】:How to get full size picture and thumbnail from Camera in the same intent如何以相同的意图从相机获取全尺寸图片和缩略图
【发布时间】:2015-08-17 05:17:42
【问题描述】:

我一直需要为这个问题找到解决方案。我已经从这个社区搜索并测试了许多解决方案,但任何人都适合帮助我。

我有两个活动,第一个活动拍摄一张照片并将其发送到另一个有一个 ImageView 来接收它(直到这里我遇到问题)和一个在数据库中插入文件路径的查询(代码做这最后一部分很好)。

我想它更适合视图加载低分辨率图像作为缩略图。因此,要保存到数据库中,我想从全尺寸图片中获取整个路径。

图像是否可能必须位于特定目录中,我可以选择给定函数参数的名称。

也许有人也知道如何自动设置相机的最小分辨率。

【问题讨论】:

    标签: android thumbnails filepath android-camera-intent


    【解决方案1】:

    如果你想拥有全尺寸的图片,你需要在 Intent 的额外数据上提供 URI。这样做,没有返回缩略图,所以你需要自己生成。

    要修改第二个 Activity,您可以使用第二个 Activity 的 onResume() 方法使用保存在数据库中的路径更新 ImageView;只需确保验证它不为空即可。

    public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    
    private boolean hasCamera(){
        return getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }
    
    public void takePicture(){
    
        // Check if there is a camera.
        if(!hasCamera())
        {
            Toast.makeText(getActivity(), "This device does not have a camera.", Toast.LENGTH_SHORT).show();
            return;
        }
    
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
        //TODO: Set this to the path you want to save, 
        //you can use Environment.getExternalStorageDirectory() 
        //and a TimeStamp to generate an unique name
        //Save the path to your DB
        Uri exampleUri  = Uri.parse("file:///sdcard/photo.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, exampleUri);
        startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
    
    
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
                // Image captured and saved to fileUri specified in the Intent
                Toast.makeText(this, "Image saved", Toast.LENGTH_LONG).show();
    
               //If you no use onResume(); to update the ImageView
               //Another way is calling here your method for update the ImageView
               //with the path of your DB
            }
            else
            {
                if (resultCode == RESULT_CANCELED)
                {
                    // User cancelled the image capture
                    // So need to Delete the path from DB
                }
            }
        }
    

    生成缩略图的简单方法:

    Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
    imageView.setImageBitmap(thumbImage);
    

    【讨论】:

      【解决方案2】:

      使用camera 意图获取全尺寸图片和缩略图的示例

      先声明一个按钮拍照...

      Button takePhoto = (Button) findViewById(R.id.btn_takePhoto);
              takePhoto.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
                          Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                          startActivityForResult(cameraIntent, 1);
                  }
              });
      

      然后在onActivityResult 我们正在获取数据...

      @Override
          public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
      
              if(requestCode == 1 && resultCode == Activity.RESULT_OK){
      
                      //path from full size image
                      Cursor cursor = MyActivity.this.getContentResolver().query(
                              MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                              new String[]{
                                      MediaStore.Images.Media.DATA,
                                      MediaStore.Images.Media.DATE_ADDED,
                                      MediaStore.Images.ImageColumns.ORIENTATION
                              },
                              MediaStore.Images.Media.DATE_ADDED,
                              null,
                              "date_added DESC");
      
                      Bitmap fullsize;
                      if (cursor != null && cursor.moveToFirst()) {
                          Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                          String photoPath = uri.toString();
                          cursor.close();
                          if (photoPath != null) {
                              System.out.println("path: "+photoPath); //path from image full size
                              fullsize = decodeSampledBitmap(photoPath);//here is the bitmap of image full size
                          }
                      }
      
                      // do whatever you want with the Bitmap .... fullsize
      
      
                      // thumbnail
                      Bitmap thumbnail;
                      thumbnail = (Bitmap) data.getExtras().get("data");
                      // do whatever you want with the Bitmap .... thumbnail
                  }
              }
      

      对于内存泄漏,请使用此函数

      private 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) {
      
                  final int halfHeight = height / 2;
                  final int halfWidth = width / 2;
      
                  // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                  // height and width larger than the requested height and width.
                  while ((halfHeight / inSampleSize) > reqHeight
                          && (halfWidth / inSampleSize) > reqWidth) {
                      inSampleSize *= 2;
                  }
              }
      
              return inSampleSize;
          } 
      
      
      
      private Bitmap decodeSampledBitmap(String pathName,
                                         int reqWidth, int reqHeight) {
      
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(pathName, options);
      
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
      
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(pathName, options);
      }
      
      //I added this to have a good approximation of the screen size:
      private Bitmap decodeSampledBitmap(String pathName) {
          Display display = getWindowManager().getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int width = size.x;
          int height = size.y;
          return decodeSampledBitmap(pathName, width, height);
      }
      

      注意: decodeSampledBitmap 是一个提高内存泄漏性能的函数,比如当你得到 OOM(内存不足)时......

      Loading Large Bitmaps Efficiently

      【讨论】:

      • 谢谢!此解决方案需要访问文件存储(API 23 或更高版本)。一个例外是:“引起:java.lang.SecurityException:权限拒绝:从pid = 6503读取com.android.providers.media.MediaProvider uri content://media/external/images/media,uid = 10060需要android。 permission.READ_EXTERNAL_STORAGE,或 grantUriPermission()"。
      猜你喜欢
      • 2011-09-20
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2018-04-21
      • 2015-08-25
      • 1970-01-01
      相关资源
      最近更新 更多