-1

How to change the shape of an uploaded image (Image from photo gallery) into circular form in android eclipse.

Adarsh
  • 89
  • 4
  • 14

1 Answers1

0

This is how you use it :

//this the button which allows you to access the gallery
pic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gallery();
        }
    });

//this allows you select one image from gallery
 public void gallery() {
    intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
//when you start activity for result and choose an image, the code will automatically continue here
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Uri current_ImageURI = data.getData();
            selectedImagePath = getPath(current_ImageURI);
            image.setImageDrawable(RoundedBitmapDrawableFactory.create(getResources(),selectedImagePath));
        } 
    }
}

public String getPath(Uri contentUri) {
// we have to check for sdk version because from lollipop the retrieval of path is different 
        if (Build.VERSION.SDK_INT < 21) {
            // can post image
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            return cursor.getString(column_index);
        } else {
            String filePath = "";
            String wholeID = DocumentsContract.getDocumentId(contentUri);

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];

            String[] column = {MediaStore.Images.Media.DATA};

            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = getActivity().getContentResolver().
                    query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            column, sel, new String[]{id}, null);


            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
            return filePath;
        }
    }

Now the two functions below : decode the sample size of the bitmap (to do correct scaling) and the other extracts the bitmap from the path are also no longer to be used

The Circular Bitmap class is no longer to be used

For better understanding please use this link fr reference : http://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawableFactory.html

Miriana Itani
  • 865
  • 9
  • 25
  • Thank you...but I'm totally new to android..so I can't understand where to put these codes..can you send me a sample code for this..It will be really helpful for me. – Adarsh Aug 02 '15 at 14:05
  • @Sindhu please check the updated answer and let me know if you need any help – Miriana Itani Aug 02 '15 at 14:41
  • @Miriani Itani : I got error in thiese lines:: (1) selectedImagePath = getPath(current_ImageURI); - ERROR:selectedImagePath cannot be resolved to a variable (2) image.setImageBitmap(c.circle(decodeSampledBitmap(new File(path), 400, 400),Color.rgb(255,255,255))); -ERROR:path and image cannot be resolved to a variable – Adarsh Aug 02 '15 at 15:07
  • @Miriani Itani: (3) Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null); -ERROR: The method getActivity() is undefined for the type MainActivity (4) Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,column, sel, new String[]{id}, null); -ERROR: The method getActivity() is undefined for the type MainActivity – Adarsh Aug 02 '15 at 15:07
  • You have to define String selectedImagePath in your activity and instead of path actually use selectedImagePath. Also instead of image use the imageView you already defined in your activity – Miriana Itani Aug 02 '15 at 15:14
  • For the cursor error please remove getActivity() . getActivity() is actually used in fragments and you are using an activity – Miriana Itani Aug 02 '15 at 15:15
  • @Miriani Itani:Yeh I have tried this code and executed without any error. But the result shows "Unfortunately the app has stopped". What might be the problem? – Adarsh Aug 02 '15 at 15:49
  • Can you please update the question with the whole code and the logcat. – Miriana Itani Aug 02 '15 at 15:50
  • @Miriani Itani:I could now select an image from gallery,but then the app crashes with "unfortunaley stopped" message. The uploded image is not displayed.Before that it crashes. – Adarsh Aug 02 '15 at 16:52
  • Logcat show: 08-02 12:44:44.322: E/StrictMode(972): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. 08-02 12:44:44.322: E/StrictMode(972): java.lang.Throwable: Explicit termination method 'release' not called 08-02 12:44:44.322: E/StrictMode(972): at dalvik.system.CloseGuard.open(CloseGuard.java:184) 08-02 12:44:44.322: E/StrictMode(972): at android.drm.DrmManagerClient.(DrmManagerClient.java:258) 08-02 12:44:44.322: – Adarsh Aug 02 '15 at 16:55
  • E/StrictMode(972): at com.google.android.mms.pdu.PduPersister.(PduPersister.java:288) 08-02 12:44:44.322: E/StrictMode(972): at com.google.android.mms.pdu.PduPersister.getPduPersister(PduPersister.java:296) 08-02 12:44:44.322: E/StrictMode(972): at com.android.mms.transaction.TransactionService.onNewIntent(TransactionService.java:224) 08-02 12:44:44.322: E/StrictMode(972): at com.android.mms.transaction.TransactionService$ServiceHandler.handleMessage(TransactionService.java:621) 08-02 12:44:44.322: – Adarsh Aug 02 '15 at 16:55
  • E/StrictMode(972): at android.os.Handler.dispatchMessage(Handler.java:102) 08-02 12:44:44.322: E/StrictMode(972): at android.os.Looper.loop(Looper.java:136) 08-02 12:44:44.322: E/StrictMode(972): at android.os.HandlerThread.run(HandlerThread.java:61) – Adarsh Aug 02 '15 at 16:56
  • 08-02 13:00:19.282: D/dalvikvm(1222): Not late-enabling CheckJNI (already on) 08-02 13:00:21.032: I/Choreographer(1222): Skipped 35 frames! The application may be doing too much work on its main thread. 08-02 13:00:21.672: D/gralloc_goldfish(1222): Emulator without GPU emulation detected. 08-02 13:00:46.862: D/dalvikvm(1222): Debugger has detached; object registry had 1 entries 08-02 13:06:41.882: D/gralloc_goldfish(1277): Emulator without GPU emulation detected. – Adarsh Aug 02 '15 at 17:10
  • I need to tell you how to do this – Miriana Itani Aug 02 '15 at 17:51
  • Okay I will guide you through this because the logcat actually is missing. First please check if you have the right permission which is: . This has to be in the manifest. (This is the place where you declare your activities and all the permissions needed). If it stays charshing then I am going to ask you to show me a specific line in the logcat. Please bear with me but I am sure of my work. – Miriana Itani Aug 02 '15 at 17:56
  • Glad I could help. Happy coding. – Miriana Itani Aug 02 '15 at 18:02
  • @Miriani Itani:I have another doubt regarding Floating Action Button . I got it working in android studio..but my requirement is in eclipse. I think it is not directly available in android eclipse. I need FAB in android eclipse – Adarsh Aug 02 '15 at 18:05
  • Can you post it as a new question with code and explanation so that I can have a better look at it. – Miriana Itani Aug 02 '15 at 18:09
  • i have posted my question here. http://stackoverflow.com/questions/31555510/floating-action-button-in-android-eclipse-which-on-clicking-will-navigate-to-ano – Adarsh Aug 02 '15 at 18:11
  • @Sindhu will check it now – Miriana Itani Aug 02 '15 at 18:11
  • @ Miriana Itani:: I have an issue in the above question- "How to change the shape of an uploaded image (Image from photo gallery) into circular form" My issue is , when i upload images of various sizes, the circular image is displayed in the image view with various size, for example, if i upload a image of smaller size, it accurately fit my image view, but if the image that i upload is larger, then the uploaded circular image exceeds the image view size. – Adarsh Aug 12 '15 at 16:00
  • @Miriani Itani: How can I rectify this so that all the images that are uploaded on the image view has the same size within the image view. Please help me to sort the problem.Otherwise, my whole work would be a waste...Please help me.. – Adarsh Aug 12 '15 at 16:05
  • Hi..I have rectified my problem..I set my width and height myself rather than wrap content..it worked..Thank you for responding..:) – Adarsh Aug 13 '15 at 04:25
  • @Sindhu anytime. Happy Coding – Miriana Itani Aug 13 '15 at 07:38
  • @Miriani Itani: Can you tell me how to send my uploaded image to php server using JSON. I have referred many tutorials, but I donno where to edit in my code.I'll post my code!.Please help me.. – Adarsh Aug 13 '15 at 08:32
  • Please open a new question for this. It would be better for the community. – Miriana Itani Aug 13 '15 at 08:34
  • You do not change, you open a new question so that people would know the correct question and answer – Miriana Itani Aug 13 '15 at 08:44