Android photography or image sorting to handle 2.1sdk compatibility issues.

Source: Internet
Author: User

After the latest project experiences different mobile phone models and SDK versions, it was found that there was a bug in the previous article titled "android2.3 selecting album images or calling the system to take photos, that is, it cannot be used on 2.1 Sony mobile phones and cannot get the native image size. Next we will record the success of the recent test, mainly by calling the album and System camera:

/** * Show Check Image Dialog */private void showheckImageDialog() {final CharSequence[] items = { getResources().getString(R.string.photo_manager_check_cameras),getResources().getString(R.string.photo_manager_check_image) };Builder builder = new AlertDialog.Builder(this);builder.setTitle(R.string.photo_manager_check_dialog_title).setItems(items, this);builder.create().show();}@Overridepublic void onClick(DialogInterface dialog, int which) {if (which == 0) { // Camerastry {// get image file save pathtemporaryPhotoPath = getImageFilePath();// user sdk camerasIntent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File captureFile = new File(temporaryPhotoPath);this.captureUri = Uri.fromFile(captureFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);startActivityForResult(intent, CAMERAMAN_IMAGE);} catch (Exception e) {Logging.e("error", e.getMessage());}} else {Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);getImage.addCategory(Intent.CATEGORY_OPENABLE);getImage.setType("image/*");startActivityForResult(Intent.createChooser(getImage, getString(R.string.photo_select_title)), SELECT_IMAGE);}}

The above temporaryphotopath is the path to save the image.

The following describes how to use the callback method to obtain the image path. You must note that you cannot use the intent parameter of the callback method to obtain the image, because the image object is reduced by the system, instead of the native image of the image.

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {Bitmap bitmap = null;String parth = null;if (data != null) {// get file pathparth = FileManager.getImagePathByIntent(this, data);}// copy fileif (StringUtil.isExistsData(parth) && !parth.equals(this.temporaryPhotoPath)) {// get file save pathif (!StringUtil.isExistsData(this.temporaryPhotoPath)) {this.temporaryPhotoPath = getImageFilePath();}FileManager.copyFile(parth, this.temporaryPhotoPath);}// get imageif (StringUtil.isExistsData(temporaryPhotoPath)) {bitmap = FileManager.converntBitmapSize(temporaryPhotoPath, 960);this.temporaryPhotoPath = null;}

In the above Code, copyfile () is used to copy an image to the specified directory. The following describes how to obtain the image path and copy a file:

/** * Get Image By Intent *  * @param data * @return */public static String getImagePathByIntent(Activity activity, Intent data) {String path = null;Cursor cursor = null;try {Uri originalUri = data.getData();path = originalUri.toString();String[] proj = { MediaStore.Images.Media.DATA };cursor = activity.managedQuery(originalUri, proj, null, null, null);if (cursor != null) {int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);cursor.moveToFirst();path = cursor.getString(column_index);}path = path.substring(path.indexOf("/sdcard"), path.length());} catch (Exception e) {Logging.e("getImageByIntent error", e.getMessage());} finally {if (cursor != null) cursor.close();}return path;}

/** * copy file *  * @param sourcePath * @param newPath * @return */public static boolean copyFile(String sourcePath, String newPath) {boolean isSuccess = false;try {File sourceFile = new File(sourcePath);if (!sourceFile.exists()) {return isSuccess;}// delete already fileFile newFile = new File(newPath);if (newFile.exists()) {newFile.delete();}// copy fileisSuccess = sourceFile.renameTo(newFile);} catch (Exception e) {Logging.e("copyFile error", e.getMessage());}return isSuccess;}

The following describes how to convert the image size and save it:

/** * convert image size *  * @param imagePath *            String image file path * @param width *            Int new image max width * @param height *            Int new image max height * @return */public static Bitmap converntBitmapSize(String imagePath, int maxSize) {Bitmap bitmap = null;int defaultMaxSize = 960;try {if (maxSize > 0) {defaultMaxSize = maxSize;}File imageFile = new File(imagePath);if (imageFile.exists()) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(imagePath, options);// the resource image max sizeint sourceMaxSize = Math.max(options.outWidth, options.outHeight);if (sourceMaxSize < 1) {return null;}int scale = sourceMaxSize / defaultMaxSize;options.inJustDecodeBounds = false;options.inSampleSize = scale;// first narrow imagebitmap = BitmapFactory.decodeFile(imagePath, options);int newMaxSize = Math.max(bitmap.getWidth(), bitmap.getHeight());if (newMaxSize > defaultMaxSize) {// second narrow imagefloat newSize = (float) defaultMaxSize / (float) newMaxSize;Matrix matrix = new Matrix();matrix.postScale(newSize, newSize);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);matrix = null;}saveImage(bitmap, imagePath);}} catch (Exception e) {Logging.e("converntBitmapSize", e.getMessage());}return bitmap;}

After testing, we have not encountered any problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.