Android practice simple tutorial-28th gun (Uri to String type instance), androiduri

Source: Internet
Author: User

Android practice simple tutorial-28th gun (Uri to String type instance), androiduri

After reading the previous article, we can easily obtain the uri of the selected image. So how can we convert the obtained uri to a String address?

Next we will study it through examples. The layout file is the same as the previous article (article 27), so we will not list it any more. Let's look at MainActivity. java:

Package com. example. userphoto; import java. io. file; import android. app. activity; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; imp Ort android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. toast; import cn. bmob. v3.Bmob; import cn. bmob. v3.datatype. bmobFile; import cn. bmob. v3.listener. uploadFileListener; public class MainActivity extends Activity implements OnClickListener {private static final int IMAGE_REQUEST_CODE = 0; private static final int CAMERA_REQUEST_CODE = 1; Private static final int RESIZE_REQUEST_CODE = 2; private static final String IMAGE_FILE_NAME = "header.jpg"; private ImageView mImageHeader; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); // initialize BmobsetupViews ();} private void setupViews () {mImageHeader = (Im AgeView) findViewById (R. id. image_header); final Button selectBtn1 = (Button) findViewById (R. id. btn_selectimage); final Button selectBtn2 = (Button) findViewById (R. id. btn_takephoto); selectBtn1.setOnClickListener (this); selectBtn2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_selectimage: Intent galleryIntent = new Intent (Intent. ACTION_GET_CONTENT); gal LeryIntent. addCategory (Intent. CATEGORY_OPENABLE); galleryIntent. setType ("image/*"); // image startActivityForResult (galleryIntent, IMAGE_REQUEST_CODE); break; case R. id. btn_takephoto: if (isSdcardExisting () {Intent cameraIntent = new Intent ("android. media. action. IMAGE_CAPTURE "); // take a photo of cameraIntent. putExtra (MediaStore. EXTRA_OUTPUT, getImageUri (); cameraIntent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 0); startAc TivityForResult (cameraIntent, CAMERA_REQUEST_CODE);} else {Toast. makeText (v. getContext (), "insert SD card", Toast. LENGTH_LONG ). show () ;} break ;}}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode! = RESULT_ OK) {return;} else {switch (requestCode) {case IMAGE_REQUEST_CODE: Uri originalUri = data. getData (); // obtain the image uriresizeImage (originalUri); // The following method converts the retrieved uri to the String type! String [] imgs = {MediaStore. images. media. DATA}; // convert the image URI to the storage path Cursor cursor = this. managedQuery (originalUri, imgs, null); int index = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); cursor. moveToFirst (); String img_url = cursor. getString (index); showToast (img_url); break; case CAMERA_REQUEST_CODE: if (isSdcardExisting () {resizeImage (getImageUri ();/* // upload the image String imgpath = getImageUr I (); final BmobFile icon = new BmobFile (new File (imgpath); icon. upload (this, new UploadFileListener () {@ Overridepublic void onSuccess () {// TODO Auto-generated method stubPerson person = new Person (); person. setIcon (icon); person. save (MainActivity. this); showToast ("image uploaded successfully") ;}@ Overridepublic void onProgress (Integer arg0) {// TODO Auto-generated method stub }@ Overridepublic void onFailure (int arg0, Strin G arg1) {// TODO Auto-generated method stubshowToast ("Image Upload Failed:" + arg1) ;}}); */} else {Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", Toast. LENGTH_LONG). show ();} break; case RESIZE_REQUEST_CODE: if (data! = Null) {showResizeImage (data) ;}break ;}} super. onActivityResult (requestCode, resultCode, data);} private void showToast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show ();} private boolean isSdcardExisting () {// determine whether the SD card has final String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false;} public void resizeImage (Uri ur I) {// reshapes the image size Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); intent. putExtra ("crop", "true"); // intent can be cropped. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", 150); intent. putExtra ("outputY", 150); intent. putExtra ("return-data", true); startActivityForResult (intent, RESIZE_REQUEST_CODE);} private void showResizeImage (Intent d Ata) {// display image Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); Drawable drawable = new BitmapDrawable (photo); mImageHeader. setImageDrawable (drawable);} private Uri getImageUri () {// get the path return Uri. fromFile (new File (Environment. getExternalStorageDirectory (), IMAGE_FILE_NAME ));}}

The method is listed as follows:

Uri originalUri = data. getData (); // obtain the image uriresizeImage (originalUri); // The following method converts the retrieved uri to the String type! String [] imgs = {MediaStore. images. media. DATA}; // convert the image URI to the storage path Cursor cursor = this. managedQuery (originalUri, imgs, null); int index = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); cursor. moveToFirst (); String img_url = cursor. getString (index); showToast (img_url );

The following error occurs when running an instance:


If you do not understand English, you can understand the problem. If you are notified that you have insufficient permissions, add the following permissions:


Run the instance successfully:


The image path is displayed in Toast. What is the purpose of obtaining this path? Please read the next article (shots ).



Thank you for your support!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.