how to get two file paths in one android Activity?

158 views Asked by At

as you can see below I'v tried to get 2 file paths in 2 different TextView by 2 different Buttons in one layout but the second TextView witch is made to show a .jpg or a.png chosen image path, placed in the internal storage, isn't showing path in it's TextView and it is giving me the toast "نوع فایل انتخابی قابل قبول نیست" witch means "invalid file type".

here is my try: first of all:

TextView pathfiletxt, pathpictxt;
Button chfilebtn, chpicbtn
private static final int SELECT_FILE_DIALOG = 1;
private static final int SELECT_IMAGE_DIALOG = 2;

in onCreate:

pathfiletxt = (TextView) findViewById(R.id.txt_pathfile);
pathpictxt = (TextView) findViewById(R.id.txt_pathpic);
chfilebtn = (Button) findViewById(R.id.btn_chosfile);
chpicbtn = (Button) findViewById(R.id.btn_chospic);

in setOnClickListeners :

    chfilebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("audio/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "فایل مورد نظر خود را انتخاب کنید:"), SELECT_FILE_DIALOG);
        }
    });

    chpicbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "فایل مورد نظر خود را انتخاب کنید:"), SELECT_IMAGE_DIALOG);
        }

    });

in onActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent result) {
        super.onActivityResult(requestCode, resultCode, result);
//      if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_FILE_DIALOG) {
                Uri data1 = result.getData();

                if(data1.getLastPathSegment().endsWith(".pdf") || data1.getLastPathSegment().endsWith(".mp3")){
                    pathfiletxt.setText(data1.getPath());
                } else {
                    Toast.makeText(RegBookActivity.this, "نوع فایل انتخابی قابل قبول نیست", Toast.LENGTH_SHORT).show();   
                }               
            }
//      }
            if(requestCode == SELECT_IMAGE_DIALOG){
                Uri data2 = result.getData();

                 if(data2.getLastPathSegment().endsWith(".jpg") || data2.getLastPathSegment().endsWith(".png")){
                     pathpictxt.setText(data2.getPath());
                    } else {
                        Toast.makeText(RegBookActivity.this, "نوع فایل انتخابی قابل قبول نیست", Toast.LENGTH_SHORT).show();   
                    }  
            }
    }

I'v tried commented codes also and many different shapes but it didn't work properly and it is just showing the Button chfilebtn chosen file's path in the TextView pathfiletxt . please help. thank you.

1

There are 1 answers

0
Niloofar Hakkaki On BEST ANSWER

this is working for me:

private Uri getUri() {
                String state = Environment.getExternalStorageState();
                if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
                    return MediaStore.Images.Media.INTERNAL_CONTENT_URI;

                return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
            super.onActivityResult(requestCode, resultCode, result);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_FILE_DIALOG) {
                    if (null == result) return;
                    Uri data = result.getData();

                    if(data.getLastPathSegment().endsWith("pdf") || data.getLastPathSegment().endsWith("mp3")){
                        sourceFileUri = data.getPath();
                        SharedPreferences upload = getSharedPreferences("uploadPrefs", 0);
                        SharedPreferences.Editor setedt = upload.edit();
                        setedt.putString("filepath", data.getPath());
                        setedt.commit();
                        pathfiletxt.setText(upload.getString("filepath", "").toString());
                    } else {
                        Toast.makeText(RegBookActivity.this, "نوع فایل انتخابی قابل قبول نیست", Toast.LENGTH_SHORT).show();   
                    }               

            }
                if(requestCode == SELECT_IMAGE_DIALOG){
                    if (null == result) return;
                    try {
                        Uri originalUri = result.getData();


                        String pathsegment[] = originalUri.getLastPathSegment().split(":");
                        String id = pathsegment[0];
                        final String[] imageColumns = { MediaStore.Images.Media.DATA };
                        final String imageOrderBy = null;

                        Uri uri = getUri();
                        Cursor imageCursor = RegBookActivity.this.getContentResolver().query(uri, imageColumns,
                                MediaStore.Images.Media._ID + "=" + id, null, null);

                        if (imageCursor.moveToFirst()) {
                            String value = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                            if(value.contains("jpg") || value.contains("png")){
                                sourceImgUri = value;
                                Bitmap bitmap = BitmapFactory.decodeFile(value);
                                upimage.setImageBitmap(bitmap);
                                SharedPreferences upload = getSharedPreferences("uploadPrefs", 0);
                                SharedPreferences.Editor setedt = upload.edit();
                                setedt.putString("picpath", value);
                                setedt.commit();
                                pathpictxt.setText(upload.getString("picpath", "").toString());
                            } else {
                                Toast.makeText(RegBookActivity.this, "نوع فایل انتخابی قابل قبول نیست", Toast.LENGTH_SHORT).show();   
                            } 
                        }

                    } catch (Exception e) {
                        Toast.makeText(RegBookActivity.this, "نوع فایل انتخابی قابل قبول نیست", Toast.LENGTH_LONG).show();
                    }

                }

            }
        }