This is how am doing, this works perfectly.
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourpackagename.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
</paths>
createImageFile()
make sure you have read and write external storage permission
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "MYAPPNAME-" + timeStamp + ".png";
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
"YourAppFolder");
File storageDir = new File(mediaStorageDir + "/Profile_Images");
if (!storageDir.exists())
{
storageDir.mkdirs();
}
File image = new File(storageDir, imageFileName);
return image;
}
click listener on button to take camera image
===Global Variables===
Uri mUri;
private static final int CAMERA_IMAGE_RESULT = 202;
private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
===Global Variables===
takeImageBTN.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
File file = null;
try
{
file = createImageFile();
mUri = FileProvider.getUriForFile(this,
CAPTURE_IMAGE_FILE_PROVIDER, file);
Log.d("uri", mUri.toString());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(cameraIntent, CAMERA_IMAGE_RESULT);
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
Then lastly onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case CAMERA_IMAGE_RESULT:
{
if (resultCode == RESULT_OK)
{
if (mUri != null)
{
Log.d("uriPath", mUri.getPath().replace("//", "/"));
String profileImageFilepath = mUri.getPath().replace("//", "/");
Log.d("path", profileImageFilepath);
profileIV.setImageURI(mUri);
/*Your Asynctask to upload Image using profileImageFilepath*/
new PostDataAsyncTask().execute(profileImageFilepath);
}
}
break;
}
}
}
take run time permission for
<!-- == External Storage == -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…