0

正如你在下面看到的,我试图在一个布局中获得 2 个不同TextView的 2 个不同Button的 2 个文件路径,但是第二个TextView女巫被用来显示一个.jpg.png选择的图像路径,放置在内部存储中,不显示路径在它里面TextView,它给了我吐司"نوع فایل انتخابی قابل قبول نیست"女巫的意思"invalid file type"

这是我的尝试:首先:

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

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);

setOnClickListener小号:

    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);
        }

    });

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();   
                    }  
            }
    }

我也尝试了注释代码和许多不同的形状,但它不能正常工作,它只是Button chfilebtnTextView pathfiletxt. 请帮忙。谢谢你。

4

1 回答 1

0

这对我有用:

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();
                    }

                }

            }
        }
于 2014-11-26T17:05:30.600 回答