从MediaStore获取正常大小

时间:2014-06-10 16:16:44

标签: android camera mediastore

我使用此代码,MediaStore以小尺寸返回bitmapimage

如何使用mediastore获取正常尺寸的照片?

有人帮我解决了这个问题吗?需要一些帮助来解决这个问题。需要正常尺寸的照片。

public class ActivityPhoto extends Activity implements OnClickListener{

    File directory;
    final int TYPE_PHOTO = 1;
    final int REQUEST_CODE_PHOTO = 1;

    ImageView imageViewPhoto;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.schedule_of_subject_photo);

        createDirectory();
        imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
    }

    public void onClickPhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, generateFileUri(TYPE_PHOTO));
        startActivityForResult(intent, REQUEST_CODE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        if (requestCode == REQUEST_CODE_PHOTO) {
            if (resultCode == RESULT_OK) {
                if (intent == null) {
                    Log.d(TAG, "Intent is null");
                } else {
                    Log.d(TAG, "Photo uri: " + intent.getData());
                    Bundle bndl = intent.getExtras();
                    if (bndl != null) {
                        Object obj = intent.getExtras().get("data");
                        if (obj instanceof Bitmap) {
                            Bitmap bitmap = (Bitmap) obj;
                            Log.d(TAG, "bitmap " + bitmap.getWidth() + " x " + bitmap.getHeight());
                            imageViewPhoto.setImageBitmap(bitmap);
                        }
                    }
                }
            } else if (resultCode == RESULT_CANCELED) {
                Log.d(TAG, "Canceled");
            }
        }  
    }

    private Uri generateFileUri(int type) {
        File file = null;
        switch (type) {
        case TYPE_PHOTO:
            file = new File(directory.getPath() + "/" + "photo_"
                    + System.currentTimeMillis() + ".jpg");
            break;
        }
        Log.d(TAG, "fileName = " + file);
        return Uri.fromFile(file);
    }

    private void createDirectory() {
        directory = new File(
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyFolder");
        if (!directory.exists())
            directory.mkdirs();
    }
}

1 个答案:

答案 0 :(得分:0)

看起来似乎无法从MediaStore获取它。

正如MediaStore.ACTION_IMAGE_CAPTURE的文档所说:

  

调用者可以传递额外的EXTRA_OUTPUT来控制此图像的位置   将写。如果EXTRA_OUTPUT不存在,则小   size图像作为额外字段中的Bitmap对象返回。这是   适用于只需要小图像的应用程序。如果   存在EXTRA_OUTPUT,然后将写入全尺寸图像   EXTRA_OUTPUT的Uri值。

因此,接收较小的图像(或者我的测试设备上根本没有图像)是预期的行为。我认为您需要自己解码图像才能获得原始尺寸。如下所示:

public class MyActivity extends Activity {

    private static final String TAG = "MyActivity";
    File directory;
    final int TYPE_PHOTO = 1;
    final int REQUEST_CODE_PHOTO = 1;

    ImageView imageViewPhoto;
    private Uri mFileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        createDirectory();
        imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar, menu);
        return true;
    }

    public void onClickPhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        mFileUri = generateFileUri(TYPE_PHOTO);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
        startActivityForResult(intent, REQUEST_CODE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        if (requestCode == REQUEST_CODE_PHOTO) {
            if (resultCode == RESULT_OK) {
                imageViewPhoto.setImageURI(mFileUri);

                new SetPictureTask(getApplicationContext(), mFileUri, imageViewPhoto).execute();
            } else if (resultCode == RESULT_CANCELED) {
                Log.d(TAG, "Canceled");
            }
        }
    }

    private Uri generateFileUri(int type) {
        File file = null;
        switch (type) {
            case TYPE_PHOTO:
                file = new File(directory.getPath() + "/" + "photo_"
                        + System.currentTimeMillis() + ".jpg");
                break;
        }
        Log.d(TAG, "fileName = " + file);
        return Uri.fromFile(file);
    }

    private void createDirectory() {
        directory = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyFolder");
        if (!directory.exists())
            directory.mkdirs();
    }

    private static class SetPictureTask extends AsyncTask<Void, Void, Void> {
        private final ImageView mImageView;
        private final Uri mFileUri;
        private final Context mContext;

        private Bitmap mBitmap;
        private int mWidth;
        private int mHeight;

        public SetPictureTask(final Context context, final Uri fileUri, final ImageView imageViewPhoto) {
            mImageView = imageViewPhoto;
            mFileUri = fileUri;
            mContext = context;
        }

        @Override
        protected Void doInBackground(final Void... params) {
            BitmapFactory.Options opts = new BitmapFactory.Options();

            opts.inJustDecodeBounds = true;

            BitmapFactory.decodeFile(mFileUri.getPath(), opts);

            mWidth = opts.outWidth;
            mHeight = opts.outHeight;

            // required width for image in order to decode faster and consume less memory
            final int scaleFactor = Math.min(mWidth / mImageView.getWidth(), mHeight / mImageView.getHeight());

            opts.inJustDecodeBounds = false;
            opts.inSampleSize = scaleFactor;
            opts.inPurgeable = true;

            mBitmap = BitmapFactory.decodeFile(mFileUri.getPath(), opts);

            return null;
        }

        @Override
        protected void onPostExecute(final Void aVoid) {
            mImageView.setImageBitmap(mBitmap);

            Toast.makeText(mContext, "Bitmap dimensions are (" + mWidth + " x " + mHeight + ")", Toast.LENGTH_LONG).show();
        }
    }
}

请注意添加AsyncTask以获取尺寸并解码Bitmap