0

请看这段代码。它只解码画廊图像而不是相机捕获的图像。如何filePath在相机捕获图像代码中指定 a ,以便它解码捕获的图像并压缩它们,并且只会解码画廊图像而不是相机捕获的图像。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    if (requestCode == GALLERY_PICTURE) {
        Uri uri = imageReturnedIntent.getData();

        if (uri != null) {
            // User had pick an image.
            Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
            cursor.moveToFirst();
            final String imageFilePath = cursor.getString(0);
            File photos = new File(imageFilePath);
            yourSelectedImage = decodeFile(photos);
            yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true);
            imageView1.setImageBitmap(yourSelectedImage);
            cursor.close();
        } else {
            Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
            toast.show();
        }
    } else if (requestCode == CAMERA_PICTURE) {
        if (imageReturnedIntent.getExtras() != null) {
            // here is the image from camera
            yourSelectedImage = (Bitmap)  
            imageReturnedIntent.getExtras().get("data");
            imageView1.setImageBitmap(yourSelectedImage);
            deleteLastCapturedImage();
        }
    }      
}
4

1 回答 1

0

您访问相机图像的方式您将始终获得您必须使用意图启动相机的图像的缩略图,如果可能,请使用此功能单击图像

public void takePhoto1() {
        if (!android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            Toast.makeText(Add_View_Images_Activity.this,
                    "Please insert SDcard for capturing photo.",
                    Toast.LENGTH_SHORT).show();
        } else {

            try {

                photo1=new File(fWrapper.path+"/"+System.currentTimeMillis()+".jpg");
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);              
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1));
                cameraIntent.putExtra("return-data", true);

                startActivityForResult(cameraIntent, 4);
            } catch (Exception e) {
                Toast.makeText(Add_View_Images_Activity.this, ""+e, Toast.LENGTH_LONG).show();
            }
        }

    }

此函数将图像保存在指定位置。现在您可以从onActivityResult中的文件photo1的路径中获取点击图像的路径。

像这样

String path=photo1.getAbsolutePath();

现在只需传递您获得此功能的路径,它始终 100% 工作。

 public Bitmap getImage(String path) throws IOException
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);        
            int srcWidth = options.outWidth;
            int srcHeight = options.outHeight;
            int[] newWH =  new int[2];
            newWH[0] = srcWidth/2;
            newWH[1] = (newWH[0]*srcHeight)/srcWidth;

            int inSampleSize = 1;
            while(srcWidth / 2 >= newWH[0]){
                srcWidth /= 2;
                srcHeight /= 2;
                inSampleSize *= 2;
            }

            //      float desiredScale = (float) newWH[0] / srcWidth;
            // Decode with inSampleSize
            options.inJustDecodeBounds = false;
            options.inDither = false;
            options.inSampleSize = inSampleSize;
            options.inScaled = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
            ExifInterface exif = new ExifInterface(path);
            String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
            Matrix matrix = new Matrix();
            float rotation = rotationForImage(Add_View_Images_Activity.this, Uri.fromFile(new File(path)));
            if (rotation != 0f) {
                matrix.preRotate(rotation);
            }
            int newh = ( w * sampledSrcBitmap.getHeight() ) /sampledSrcBitmap.getWidth();
            Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true);
            Bitmap resizedBitmap = Bitmap.createBitmap(
                    r, 0, 0, w, newh, matrix, true);

            return resizedBitmap;
        }
于 2013-03-05T10:27:35.277 回答