我已经阅读了很多关于这个问题的问题.有些类似于我所经历的;我没有成功地尝试过答案.该代码适用于HTC
Android 4.2,不适用于Nexus 7 Android 4.4.我已经修改了存储目录以在android 4.4上运行,所以这不是问题.
如果我使用相机意图永远不会返回
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
如果我使用,它会返回
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoFile);
但它不保存文件.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(manager) != null) { try { final File photoFile = createImageFile(); if (photoFile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile)); //takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoFile); startActivityForResult(takePictureIntent,1); } } catch (IOException e) { e.printStackTrace(); } }
文件名
private File createImageFile() throws IOException { if (mStorageDirectory == null) { createInitialStorageDirectory(); setupFolders(); mScrollList.notifyDataSetInvalidated(); } // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "slide_" + timeStamp; File storageDir = new File(mStorageDirectory); File image = File.createTempFile(imageFileName,".jpg",storageDir); // image.setWritable(true,false); // Save a path for use with ACTION_VIEW intents mCurrentPhotoPath = image.getAbsolutePath(); return image; }
回调函数
@Override public void onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); if (requestCode == 1 && resultCode == -1) { } }
根目录和保存目录
static String mBasePath = "/slides/"; private String getRootDirectory() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { if (Build.VERSION.SDK_INT >= 19) return mView.getContext().getFilesDir() + mBasePath; else return Environment.getExternalStorageDirectory() + "/Android/data/com.hdms.manager" + mBasePath; //return Environment.getExternalStorageDirectory() + mBasePath; } return mBasePath; }
任何想法将不胜感激.