从内部目录获取文件路径捕获照片

时间:2016-06-16 13:06:11

标签: android filepath photos image-capture

我创建了一个应用程序,在拍照后,将其上传到服务器上(使用文件路径,如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);
        }
    }

3 个答案:

答案 0 :(得分:1)

使用意图

打开相机时使用此功能
 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;
    }

答案 1 :(得分:0)

捕获图像后,您将获得ActivtyResult

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

答案 2 :(得分:0)

我认为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" />