Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Android upload image from gallery to server

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button)findViewById(R.id.Button01);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                openGallery(SELECT_FILE1);
            }
        });
    }

    public void openGallery(int req_code) {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                               "Select file to upload "), req_code);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();

            if (requestCode == SELECT_FILE1) {
                selectedPath1 = getPath(selectedImageUri);
                System.out.println("selectedPath1 : " + selectedPath1);
            }

            if (requestCode == SELECT_FILE2) {
                selectedPath2 = getPath(selectedImageUri);
                System.out.println("selectedPath2 : " + selectedPath2);
            }

            tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
        }
    }

    public String getPath(Uri uri) {

        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        return cursor.getString(column_index);
    }
Comment

how to upload image from android app to server

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1)
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();

            String filePath = getPath(selectedImage);
            String file_extn = filePath.substring(filePath.lastIndexOf(".") + 1);
            image_name_tv.setText(filePath);

            try {
                if (file_extn.equals("img") || file_extn.equals("jpg") || file_extn.equals("jpeg") || file_extn.equals("gif") || file_extn.equals("png")) {
                    //FINE
                } else {
                    //NOT IN REQUIRED FORMAT
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

public String getPath(Uri uri) {
    String[] projection = {MediaColumns.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    column_index = cursor
            .getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    imagePath = cursor.getString(column_index);

    return cursor.getString(column_index);
}
Comment

upload photo to server and view in api android

implementation 'com.squareup.retrofit2:retrofit:2.3.0'implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
Comment

PREVIOUS NEXT
Code Example
Java :: java null 
Java :: Java read in 
Java :: maths.random in Java 
Java :: Simple For Loop Java Example 
Java :: how to make a rest api in spring 
Java :: one line hashmap java 
Java :: java loop through array 
Java :: djava days between two dates 
Java :: java packages example 
Java :: sort java array 
Java :: qr code spring boot 
Java :: string array to string stream java 
Java :: java dictionary initialization 
Java :: Java Remove Elements from HashSet 
Java :: mongorepository spring boot custom query 
Java :: control structures in java 
Java :: how to start array index from 1 in java 
Java :: what is construct in java 
Java :: java float data type 
Java :: declare matrix in java 
Java :: menu alert dialog in android 
Java :: math.pow(x2-x1,2) 
Java :: configure JWT on Springboot 
Java :: Find Length of String using length() 
Java :: join strings in java 
Java :: Java How to use NavigableMap? 
Java :: Multi basic auth with spring security 
Java :: set java time complexity 
Java :: set the content of a Jlist from an other Jlist (Swing) 
Java :: how to open material navigation view on button click 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =