DEV Community

Discussion on: Help with code JAVA

Collapse
 
sac232 profile image
Sac • Edited
Button btnAddFile, btnSend;
GridView listViewFiles;

ArrayList<Uri> arrayUri = new ArrayList<> ( );
ArrayAdapter<Uri> myFileListAdapter;

final int RQS_LOADIMAGE = 0;
final int RQS_SENDEMAIL = 1;
private int MY_PERMISSIONS_REQUEST_READ_MEDIA_;
private int MY_REQUEST_CODE;
private int PICK_IMAGE_MULTIPLE;
public GridView gvGallery;
private ArrayList<String> imagesEncodedList;
private String imageEncoded;
private GalleryAdapter galleryAdapter;
private ArrayList uri;
private File fileIn;
private Uri imageUri;
private int SELECT_IMAGE;
private Uri fileUri;
private boolean imagebitmap;
private boolean isImage;
private String file;
private Uri mArrayUri;
private boolean allPath;


@SuppressLint("WrongViewCast") @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.gallery );


    gvGallery = (GridView) findViewById ( R.id.gv );


    int permissionCheck = ContextCompat.checkSelfPermission ( this ,
                                                              Manifest.permission.WRITE_EXTERNAL_STORAGE );

    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions ( this ,
                                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE} ,
                                            MY_PERMISSIONS_REQUEST_READ_MEDIA_ );
    } else {
        readDataExternal ( );
    }

    btnAddFile = findViewById ( R.id.add );
    btnSend = findViewById ( R.id.async );
    btnAddFile.setOnClickListener ( btnAddFileOnClickListener );
    btnSend.setOnClickListener ( btnSendOnClickListener );

    myFileListAdapter = new ArrayAdapter<> (
            Gallery.this,
            android.R.layout.simple_expandable_list_item_2,
            arrayUri);
    listViewFiles = findViewById ( R.id.gv );
    listViewFiles.setAdapter ( myFileListAdapter );
}


private void readDataExternal( ) {


}


View.OnClickListener btnAddFileOnClickListener
        = new View.OnClickListener ( ) {


     public void onClick(View v) {
        /*Intent intent = new Intent ( Intent.ACTION_GET_CONTENT,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.setType("image/*");
        startActivityForResult ( Intent.createChooser ( intent,"Select Picture" ), RQS_LOADIMAGE );*/

        /*Intent intent = new Intent();
        intent.setType("video/*, image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Images"), PICK_IMAGE_MULTIPLE);*/




        Intent intent2 = new Intent (Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult (intent2, PICK_IMAGE_MULTIPLE);
    }


};






View.OnClickListener btnSendOnClickListener
        = new View.OnClickListener ( ) {

    @Override
    public void onClick(View v) {
        sendAttachments ( );


    }


};


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult (requestCode, resultCode, data);



    try {
        // When an Image is picked
        if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            imagesEncodedList = new ArrayList<String>();
            if(data.getData()!=null){

                Uri mImageUri=data.getData();

                // Get the cursor
                Cursor cursor = getContentResolver().query(mImageUri,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imageEncoded  = cursor.getString(columnIndex);
                cursor.close();

                ArrayList<Uri> uris  = new ArrayList<Uri>();
                uris .add(mImageUri);
                galleryAdapter = new GalleryAdapter(getApplicationContext(),uris );
                gvGallery.setAdapter(galleryAdapter);

            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    if (data.getClipData() != null) {
                        ClipData mClipData = null;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                            mClipData = data.getClipData();
                        }
                        ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
                        for (int i = 0; i < mClipData.getItemCount(); i++) {

                            ClipData.Item item = mClipData.getItemAt(i);
                            Uri uri = item.getUri();
                            mArrayUri.add(uri);
                            // Get the cursor
                            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                            // Move to first row
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            imageEncoded  = cursor.getString(columnIndex);
                            imagesEncodedList.add(imageEncoded);
                            cursor.close();

                            galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
                            gvGallery.setAdapter(galleryAdapter);

                        }
                        Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
                    }
                }
            }
        } else {
            startActivity (new Intent(this, Finished.class));
        }
    } catch (Exception e) {
        Toast.makeText ( this , "Something went wrong" , Toast.LENGTH_LONG )
                .show ( );
    }


    if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RQS_LOADIMAGE:
                    Uri imageUri = data.getData ( );
                    arrayUri.add (imageUri);
                    myFileListAdapter.notifyDataSetChanged ( );
                    break;
                case RQS_SENDEMAIL:
                    break;


            }

         /* Toast.makeText(Gallery.this, "Stored Your Image", Toast.LENGTH_LONG)
                    .show();*/
    }

    }











private void sendAttachments() {


    String text1 = getIntent ( ).getStringExtra ("text");
    String text2 = getIntent ( ).getStringExtra ("text1");
    String text3 = getIntent ( ).getStringExtra ("text2");
    String vin = mVin.getText ( ).toString ( );
    String make = mMake.getText ( ).toString ( );
    String model = mModel.getText ( ).toString ( );
    String yr = mYr.getText ( ).toString ( );
    String cond = mCondition.getText ( ).toString ( );
    String[] emailAddressList = {"sarkisdx@gmail.com"};
    String mycontent;
    mycontent = vin + "|" + make + "|" + model + "|" + yr + "|" + cond + "|" + text1 + "|" + text2 + "|" + text3;
    StringBuilder new_string = new StringBuilder ( );
    for (String i : mycontent.split (Pattern.quote ("|"))) {
        new_string.append (i);
        new_string.append ("\n");
    }



    Intent email = new Intent (Intent.ACTION_SEND_MULTIPLE);
    email.setType ("plain/text");
    email.setType ("image/*");
    email.putExtra (Intent.EXTRA_TEXT, new_string.toString ( ));
    email.putExtra(Intent.EXTRA_STREAM, arrayUri);
    email.setFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION);
    email.addFlags (Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    ArrayList<Uri> uris = new ArrayList<>();
    email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);



    if (arrayUri.isEmpty ( )) {
        //Send email without photo attached
        email.setAction (Intent.ACTION_SEND);
        email.setType ("plain/text");
    } else if (arrayUri.size ( ) == 1) {
        //Send email with ONE photo attached
        email.setAction (Intent.ACTION_SEND);
        email.putExtra (Intent.EXTRA_STREAM, arrayUri.get (0));
        email.setType ("image/*");
    } else {
        //Send email with MULTI photo attached
        email.setAction (Intent.ACTION_SEND_MULTIPLE);
        email.putParcelableArrayListExtra (Intent.EXTRA_STREAM, arrayUri);
        email.setType ("image/*");


    }

    startActivityForResult(Intent.createChooser(email, "Sending multiple attachment"), RQS_SENDEMAIL);

}

}

Collapse
 
sac232 profile image
Sac

Thank u For replying much appreciated

the code above i can choose images 1x1 and will work but if i change this value here to choose multiple in gallery images will all turn white and u can choose multiple but wont attach to email

thanks again for your help and reply's

Thread Thread
 
sergix profile image
Peyton McGinnis

Can you point out which section of the code you're referring to?

Also I'm confused as to what the application does. Can you briefly explain? And do you know if the code uses any special libraries?

Thread Thread
 
sac232 profile image
Sac • Edited

yes bro so what this project does it u can choose images from android gallery multiple then it shows it in a imageview then you would press send and it will ask what email would u like to open with (i use gmail) it will take the images and text and add it to attachments in gmail automatically

libraries it uses yes
gallery adapter the code for that is here :

p

Thread Thread
 
sergix profile image
Peyton McGinnis

Sadly, I'm not really sure how to help here. If this is intended to be an Android app, I would take a look at the Android developer documentation here.

I wish I could assist further but I'm not very familiar with these APIs.