Get uri from camera intent in android

when you press

takePic.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); imageUri = getImageUri(); m_intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(m_intent, REQUEST_IMAGE_CAPTURE); } }); 

As a result:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Log.d("test1",""+imageUri); Intent shareIntent = new Intent(this, SharePicForm.class); shareIntent.putExtra("photo",""+imageUri); startActivity(shareIntent); } } 

getImageUri ()

 private Uri getImageUri(){ Uri m_imgUri = null; File m_file; try { SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); m_curentDateandTime = m_sdf.format(new Date()); m_imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + m_curentDateandTime + ".jpg"; m_file = new File(m_imagePath); m_imgUri = Uri.fromFile(m_file); } catch (Exception p_e) { } return m_imgUri; } 

What I would like to achieve is very simple, cause the camera’s intention and get the uri of the photo result. But it seems that there is an incompatible device, and it doesn’t work at all on my device. I tried to save the path in a public variable, but when I extract it, it is zero, is there a formal and standard way to implement it and should work on any device? Also, is there a way to not provide a custom path, but get the default uri path from the camera intent?

thanks for the help

+9
android android-intent camera
source share
3 answers

If your device does not match cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , you can still use

 Uri imageUri = data.getData(); 

in onActivityResult(int requestCode, int resultCode, Intent data) . But in most cases, the problem is that RAM is limited and the system destroys your activity in order to allocate enough memory for the camera application to fulfill your intentions. Therefore, when the result returns, the fields of your activity are not initialized, and you must follow the Amit clause and implement onSavedInstance() and onRestoreInstanceState() .

+10
source share

First of all, make sure your directory is created ......

 final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/Folder/"; File newdir = new File(dir); newdir.mkdirs(); 

when a button is pressed, this function is called.

 private void capturarFoto() { String file = dir+DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString()+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) {} Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("Demo Pic", "Picture is saved"); } } 

Make sure you add permission to the manifest

 <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+4
source share

just use this Uri imageUri = data.getData ();

-7
source share

All Articles