【问题标题】:File path to String字符串的文件路径
【发布时间】:2013-02-20 10:09:15
【问题描述】:

我正在尝试将图像文件的完整路径作为字符串获取,但它不起作用。我只是得到像“内容:/外部/媒体/图像/1”这样的结果。这绝对不是正确的道路。如何获得正确的路径,包括文件扩展名?

这是我目前尝试过的:

       public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.btnGetImage:
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                     intent.setType("image/*");
                     startActivityForResult(Intent.createChooser(intent, "Select A Picture"),  
                               PHOTO_GALLERY);
                    break;
         }
 }

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         switch(requestCode){
         case PHOTO_GALLERY:
             if (resultCode == RESULT_OK) {
                 File file = new File(data.getDataString());
                 String imagePath = file.getAbsolutePath();
                 break;
             }
        }
}

【问题讨论】:

    标签: android file path


    【解决方案1】:

    试试这个代码:

    case R.id.btnGetImage:
    
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
            break;
    

        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
    
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    
        }
    

    picturePath 是你需要的路径...

    【讨论】:

      【解决方案2】:
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           switch(requestCode){
           case PHOTO_GALLERY:
               if (resultCode == RESULT_OK) {
                   Uri selectedImageUri = data.getData();
              String selectedImagePath = getRealPathFromURI(selectedImageUri);
                   File file = new File(selectedImagePath );
      
                   break;
               }
          }
       }
      

      【讨论】:

      • 你试过这个代码,如果意图来自画廊,如果你从文件管理器中获得绝对路径,它将起作用
      【解决方案3】:

      您已经为您的路径定义了正确的filename,该路径必须存在于您的 sdcard 中,同时从 sdcard 中获取,

      字符串文件路径 = Environment.getExternalStorageDirectory()+"/foldername/"+test.png;

      我闻到你在尝试做类似this 之类的事情。

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 2020-04-14
        • 2016-03-04
        • 1970-01-01
        • 1970-01-01
        • 2018-03-08
        相关资源
        最近更新 更多