【问题标题】:Get file path from internal directory capturing photos从捕获照片的内部目录获取文件路径
【发布时间】:2018-10-16 22:13:48
【问题描述】:

我正在创建一个应用程序,它在拍照后将其上传到服务器上(使用文件路径,如 https://trinitytuts.com/capture-image-upload-server-android/ 上的教程)。

为了拍照,我按照https://developer.android.com/training/camera/photobasics.html 上的说明进行操作。

虽然我捕获具有外部存储的图像,但一切正常,但如果我使用没有 SD 的设备(如 Nexus)应用程序崩溃。

你能帮帮我吗? 谢谢

编辑

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Log.w("error", "ERROR");
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.application.package.fileprovider",//here I put the app pakage
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

【问题讨论】:

  • 发布您的相机意图代码

标签: android filepath photos image-capture


【解决方案1】:

使用intent打开相机时使用这个

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Log.e("Log Excepton", ex.toString() + "...");
        }
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, IMAGE_CAPTURE);
        }

    }

在活动结果中使用它

if (requestCode == IMAGE_CAPTURE) {
   Log.e("Log mCurrentPhotoPath", mCurrentPhotoPath + "...");
   iv_uploadimage.setImageURI(Uri.parse(mCurrentPhotoPath));
   selectedImagePath1 = mCurrentPhotoPath.substring(5);
   Log.e("Log Optimized path", selectedImagePath1 + "...");}

添加文件写入权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是用于创建文件

 private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

【讨论】:

    【解决方案2】:

    我认为createImageFile 代码有问题。

    试试这个更新的功能

    /**
     * Function for create file in SD card or internal storage depending on SD card availability
     * @return : file in internal / external storage
     */
    private File createImageFile() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // get path of pictures folder from SD card
            // give any path of your choice
            return new File(Environment.DIRECTORY_PICTURES);
        }else {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            // create folder of your app or get already exists folder
            // ie path from internal storage
            return cw.getDir("media", Context.MODE_PRIVATE);
        }
    }
    

    别忘了加permission

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    【讨论】:

      【解决方案3】:

      捕获图像后,您将在ActivtyResult 中获得结果。

      if (requestCode == CAMERA_REQUEST) {  
           Bitmap photo = (Bitmap) data.getExtras().get("data");
           ...
      } 
      

      【讨论】:

        猜你喜欢
        • 2016-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 1970-01-01
        • 1970-01-01
        • 2015-10-29
        相关资源
        最近更新 更多