as you can see below I'v tried to get 2 file paths in 2 different TextView
by 2 different Button
s 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 setOnClickListener
s :
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.
this is working for me: