Camera intent does not return to defiant activity

I read a lot of questions regarding this issue. Some of them are similar to what I experience; I tried the answers without any success. The code works on HTC Android 4.2 and does not work on Nexus 7 Android 4.4. I changed the storage directory to work with Android 4.4, so this is not a problem.

The purpose of the camera will never return if I use

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 

and returns if I use

 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile); 

but it does not save the file.

Here is the complete code. Evoke intention

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

File name

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

Callback function

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == -1) { } } 

Root directory plus save directory

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

Any thoughts would be appreciated.

+6
source share
2 answers

So, I found a solution to my problem. If the file does not exist, camera activity will not return. I think the reason it doesn't work in 4.4 is because of a change in the file system. I do not save the image in the media gallery and upload the file back to my application directory. Then the media file is deleted. The reason I don't leave it in the media directory is when the application is deleted, so the images will be deleted.

Here is the new code. Challenge challenge first

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(manager) != null) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, "New Picture"); values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera"); Uri mImageUri = App.mApplication.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); mCurrentPhotoPath = mImageUri.getPath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); startActivityForResult(takePictureIntent, 1); } 

Then callback.

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == -1) { if (data != null) mCurrentPhotoPath = String.valueOf(data.getData()); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = App.mApplication.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null); if (cursor == null) return; // find the file in the media area cursor.moveToLast(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); File source = new File(filePath); cursor.close(); // create the destination file String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "slide_" + timeStamp; File destination = new File(mStorageDirectory, imageFileName + ".jpg"); // copy the data if (source.exists()) { try { FileChannel src = new FileInputStream(source).getChannel(); FileChannel dst = new FileOutputStream(destination).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); source.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } 
+6
source

In my case, it did not return, because the output file was inside a folder that I had not yet created.

If this is the case for someone else, do it before starting the movement:

 file.getParentFile().mkdirs(); 
+6
source

Source: https://habr.com/ru/post/971581/


All Articles