How to delete SDcard file in Android - android

I tried to delete the image file from the gallery, but it won't. Image files are output normally, and sharing functions are done. It can't write and delete in my App. Files are deleted from the default app.
I tried to delete it use File class and ContentResolver. but file has not been deleted.
Android targetSdkVersion is 26 and compileSdkVersion is 28.
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I get SDcard path from getSDcardPath()
public String getSDcardPath(Context context) {
File[] storage = ContextCompat.getExternalFileDirs(context, null);
if(storage.length > 1 && storage[0] != null && storage[1] != null)
return storage[1].toString();
else
return "";
}
File Class code Used
public void useFileClass() {
File mFile = new File("file Parent + file NAME");
if (mFile.exists()) {
mFile.delete();
}
}
ContentResolver code Used
public void useContentResolver(Context context, File mFile) {
ContentResolver contentResolver = context.getContentResolver();
Uri mUri = getUri(context, mFile);
contentResolver.delete(mUri, null, null);
}
public Uri getUri(Context context, File mFile) {
Uri mUri;
mUri = FileProvider.getUriForFile(context, "MyApplication", mFile);
return mUri;
}
share code
public void shareImage() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, getUri(this, mFile));
startActivity(Intent.createChooser(shareIntent, "Share image too..."));
}

You need request Permission before access into storage. Try this:
private static final int MY_WRITE_STORAGE_PERMISSION_CODE = 200;
private void checkPermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_WRITE_STORAGE_PERMISSION_CODE);
} else {
// Todo (Add, Delete, Edit, ...)
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == MY_WRITE_STORAGE_PERMISSION_CODE)
{
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
// Todo (Add, Delete, Edit, ...)
} else {
// Permission Deny
}
}
}
Hope this help you.

Related

How to get media item real_Path from contentResolver.openFileDescriptor(Uri uri, String s) in Android Q?

MediaStore.MediaColumns.DATA constant was deprecated in API level Q.
Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access. This value will always be NULL for apps targeting Build.VERSION_CODES.Q or higher.
This is true we can't get the real path. Just Simple method open stream with contentResolver() and copy the whole content of file into new file and for getting file information we need to call a query() method of a getContentResolver().query() then we can get DISPLAY_NAME of the file and some more info like FILE_SIZE.
Simple code Example for those who upvoted the question:
public class MainFragment extends Fragment {
private Button openGallery;
private File selectedFile;
private Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
context = container.getContext();
return inflater.inflate(R.layout.fragment_question, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
openGallery = view.findViewById(R.id.openGallery);
openGallery.setOnClickListener(v->browseFile());
}
private void browseFile() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
openFiles();
} else {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 786);
}
}
private void openFiles() {
deleteFileFromCacheDir();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (intent.resolveActivity(context.getPackageManager()) != null) {
startActivityForResult(intent, 786);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 786 && resultCode == -1 && data != null) {
Uri uri = data.getData();
openStreamAndCopyContent(uri); // Here i am just copy the content of file and paste it into my new file. You can check the type of the file image/video/audio & do it whatever you want
// Now here is your file
if (selectedFile != null && selectedFile.exists()){
// Do it whatever you want Or send it to server
}
}
}
private void openStreamAndCopyContent(Uri uri) {
try {
String fileName = "temp" + System.currentTimeMillis() + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
selectedFile = new File(context.getCacheDir().getAbsolutePath() + File.separator + fileName);
InputStream inputStream = context.getContentResolver().openInputStream(uri);
if (inputStream != null) {
Utility.copy(inputStream, selectedFile);
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 786) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openFiles();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED && getActivity() != null) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(context).setTitle(R.string.permission_required).setMessage(R.string.permission_message)
.setPositiveButton(R.string.open_settings, (dialog, which) ->
context.startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + BuildConfig.APPLICATION_ID)))).setNegativeButton(R.string.close, null).show();
}
}
}
}
private void deleteFileFromCacheDir() {
if (selectedFile != null && selectedFile.exists()) {
if (selectedFile.delete()) {
selectedFile = null;
}
}
}
#Override
public void onDestroyView() {
deleteFileFromCacheDir();
super.onDestroyView();
}
}
I believe it is not possible, as you simply don't need that path from a programming stand point.
You cannot read/change/delete the file with the real path. You should use a ContentResolver for these operations as stated in your question.
If you just want to show the path to the user, then I would suggest to either give an option to open/share the file with an Intent.createChooser or just show the MediaColumns.RELATIVE_PATH.

Not able to place a image in ImageView After taking a pic from camera only in Pixel_Api_26 emulator

I am trying to take a pic from cam in my app, but it is working fine in but not able to display it in imageview it's throwing error..
please help me
my code is
It is giving error at the time of Decoding bitmapUri (i mentioned in code)
the error is:
private void onCaptureImageResult(Intent data) {
Log.w("CAM","capture image result");
launchMediaScanIntent();
try {
File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
Log.w("CAM","decoding bitmap uri");
Context context=FSE_login.this;
Bitmap bitmap = decodeBitmapUri(this, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".lerainfotech.tezmoney.FSE_login", photo));
Log.w("CAM","decoded");
attendance_pic = Utils.getResizedBitmap(bitmap);
Log.w("CAM","attached");
attendance_pic_field.setImageBitmap(attendance_pic);
} catch (Exception e) {
Log.w("Error Is",e);
Utils.showErrorDialog(getApplicationContext(), "Couldn't load image");
return;
}
}
First you need to write below code in Mainifest
<uses-permission-sdk-23 android:name="android.permission.CAMERA" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Your Activity should like this
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}else{
captureImage();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
captureImage();
}
}
}
private void captureImage(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CAMERA);
}

Problems with accessing camera with targetSdkVersion 25?

I have an app that uses camera it works fine when I compile it with targetSdkVersion 23, but when I try to use version 25 I get this error:
android.os.FileUriExposedException:
file:///storage/emulated/0/DCIM/IMG_1093948364.jpg exposed beyond app
through ClipData.Item.getUri()
This is the code that I'm using:
private void showCameraAction() {
if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
getString(R.string.mis_permission_rationale_write_storage),
REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
}else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
try {
mTmpFile = FileUtils.createTmpFile(getActivity());
} catch (IOException e) {
e.printStackTrace();
}
if (mTmpFile != null && mTmpFile.exists()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
startActivityForResult(intent, REQUEST_CAMERA);
} else {
Toast.makeText(getActivity(), R.string.mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), R.string.mis_msg_no_camera, Toast.LENGTH_SHORT).show();
}
}
}
private void requestPermission(final String permission, String rationale, final int requestCode){
if(shouldShowRequestPermissionRationale(permission)){
new AlertDialog.Builder(getContext())
.setTitle(R.string.mis_permission_dialog_title)
.setMessage(rationale)
.setPositiveButton(R.string.mis_permission_dialog_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{permission}, requestCode);
}
})
.setNegativeButton(R.string.mis_permission_dialog_cancel, null)
.create().show();
}else{
requestPermissions(new String[]{permission}, requestCode);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == REQUEST_STORAGE_WRITE_ACCESS_PERMISSION){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
showCameraAction();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
What should I do? Thanks.
Your FileUtils.createTmpFile(getActivity()); probably uses file:// URI to share file with other app(in your case camera).
Android versions greater then 24 use content:// URIs instead, and will throw this exception when you try to share a file directly using the file:// URI.
A content URI allows you to grant read and write access using temporary access permissions
Take a look at FileProvider.
SOLUTION:
Changed this:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
To this:
//getActivity() because its a fragment
Uri uri = FileProvider.getUriForFile(getActivity(),
getActivity().getPackageName()
, mTmpFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
And it worked.

Android app cache required storage permissions

I have some confusion for the android cache, at run time android app cache required external storage read and write permissions or not. can any one help me for this confusion. For example,
File tempFile = File.createTempFile("img", ".png", getExternalCacheDir());
String imgPath = tempFile.getAbsolutePath();
Try this,
Storage permission
private static final int REQUEST_STORAGE = 112;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
} else {
File tempFile = File.createTempFile("img", ".png", getExternalCacheDir());
imgPath = tempFile.getAbsolutePath();
}
} else {
File tempFile = File.createTempFile("img", ".png", getExternalCacheDir());
imgPath = tempFile.getAbsolutePath();
}
get Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
File tempFile = File.createTempFile("img", ".png", getExternalCacheDir());
imgPath = tempFile.getAbsolutePath();
}
}
}
}
check permissions for marshmallow
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
You have to ask permission at runtime https://developer.android.com/training/permissions/requesting.html, Example,
public void requestPermissionForExternalStorage() {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
}
}

android6.0 can't get gallery path , I have got uses-permission

I want to get photo from my photo gallery to crop it, but the path is null. Andorid5.0 can use this way, but Android 6.0 and Android 7.0 can't use this way. I have got this app permission.
public void initPop(View view) {
albums = (TextView)view.findViewById(R.id.albums);
cancel = (LinearLayout) view.findViewById(R.id.cancel);
albums.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
openAlbumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(openAlbumIntent, PHOTOZOOM);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Uri uri = null;
switch (requestCode) {
case PHOTOZOOM:
if (data == null) {
return;
}
uri = data.getData();
String[] proj = {
MediaStore.Images.Media.DATA
};
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
Log.i("Ienning", "onActivityResult: the cursor is " + column_index);
path = cursor.getString(column_index);
}
Intent intent3 = new Intent(PersonCenter.this, ClipActivity.class);
intent3.putExtra("path", path);
Log.i("Ienning", "The Path is " + path);
startActivityForResult(intent3, IMAGE_COMPLETE);
break;
case IMAGE_COMPLETE:
final String temppath = data.getStringExtra("path");
editor.putString("temppath", temppath);
editor.commit();
head.setImageBitmap(getLoacalBitmap(temppath));
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
And permission code:
public void getpermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
new AlertDialog.Builder(this)
.setMessage("get permission")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(PersonCenter.this, new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS}, MY_PERMISSIONS_REQUEST_WRITE_STORAGE);
}
}).show();
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS}, MY_PERMISSIONS_REQUEST_WRITE_STORAGE);
}
}
else {
Log.i("Ienning", " this is ok manifest permission");
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Ienning", "onRequestPermissionResult: the result permission is ok!");
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "permission denied!", Toast.LENGTH_SHORT).show();
}
}
return;
}
}
And loginfo is
10-28 19:39:44.424 25265-25265/com.example.ienning.ncuhome I/Ienning: onActivityResult: the cursor is 0
10-28 19:39:44.425 25265-25265/com.example.ienning.ncuhome I/Ienning: The Path is null
Andorid5.0 can use this way
You did not test it very well. Your approach will fail on all Android devices, at least some of the time. It will fail more frequently on Android 6.0+.
Your code makes two invalid assumptions:
You assume that the Uri that comes back from ACTION_GET_CONTENT has something to do with the MediaStore. This is incorrect. The Uri that comes back from ACTION_GET_CONTENT can be anything that the user-selected activity wants to return. All that is more-or-less guaranteed is that you can use ContentResolver and openInputStream() to read the content.
You assume that the MediaStore will always give you a DATA column that is usable. That is not a requirement, even if the MediaStore happens to know about the Uri (see the previous bullet).
If the scheme of the Uri that you get back from ACTION_GET_CONTENT is file, then getPath() will be a filesystem path. You may be able to use that path (otherwise, it is a bug in the third-party app that gave you that Uri).
More commonly, the scheme of the Uri will be content. In that case, you can use ContentResolver and openInputStream() to read in the content identified by that Uri, but there is no required filesystem path behind that Uri. That Uri can point to anything the other developer wants: BLOB columns in databases, files in locations that you cannot access, data that needs to be downloaded because it is not yet on the device, etc.

Categories

Resources