Friday, April 10, 2020

My Quick Find Gallery (Part 5 - Share, Delete and Update Image)

12) Share Image
If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.
Steps to replace file:// URI with content:// URI:
i) Add a FileProvider <provider> tag in AndroidManifest.xml under <application> tag:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
         <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myfileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider_paths" />
         </provider>
    </application>
</manifest>
ii) Then create a file_provider_paths.xml file in res/xml folder:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="share" path="/" />
</paths>
iii) ShowPhoto.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
       case R.id.action_share:
         File file = new File(searchFilesPath.get(selectedImagePosition));
         Intent shareIntent = new Intent(Intent.ACTION_SEND);
         Uri picUri = FileProvider.getUriForFile(this, "com.myfileprovider", file);
         shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         shareIntent.setType("image/*");
         shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
         startActivity(Intent.createChooser(shareIntent, "Share with"));
         break;
       default:
         break;
     }
     return true;
}
13) Delete Image
Call function alert() on Delete Button in ShowPhoto.java
public void alert(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Delete Photo");
        alertDialogBuilder
                .setMessage("are you sure you want to delete ?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        delete();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

}
public void delete(){
        File fdelete = new File(searchFilesPath.get(selectedImagePosition));
        if (fdelete.exists()) {
            if (fdelete.delete()) {
//Toast.makeText(this, "Successfully Deleted", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            } else {
                Toast.makeText(this, "File not deleted", Toast.LENGTH_LONG).show();
            }
        }
}
14) Update Image
Call function alert() on Update Button in EditPhoto.java
public void alert(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Update Tag or Photo");
        alertDialogBuilder
                .setMessage("are you sure you want to update ?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        update();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

}
public void update(){
        String tag=tag_editText.getText().toString();
        if(tag.matches("")) {
            Toast.makeText(this, "Please Enter Tag", Toast.LENGTH_LONG).show();
        }
        else {
            if(replaceToInternalStorage(bitmap,tag.trim())) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
            else {
                Toast toast = Toast.makeText(this, "File not updated", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
}
private boolean replaceToInternalStorage(Bitmap bitmapImage, String filename) {
        if (filename.equals(editFileName) && isImageChange == 0)
            return true;
        else if (!filename.equals(editFileName) && isImageChange == 0) {
            File oldFilePath = new File(editFilePath);
            File directory = oldFilePath.getParentFile();
            File newFilePath = new File(directory, filename + ".jpeg");
            int i = 2;
            while (newFilePath.exists()) {
                newFilePath = new File(directory, filename + "[" + i + "].jpeg");
                i++;
            }
            oldFilePath.renameTo(newFilePath);
            return true;
        }
        else if (isImageChange == 1) {
            if(delete(editFilePath)) {
                File mypath = new File(editFilePath);
                if(!filename.equals(editFileName)){
                    File oldFile = new File(editFilePath);
                    File directory = oldFile.getParentFile();
                    File newFile = new File(directory, filename + ".jpeg");
                    int i = 2;
                    while (newFile.exists()) {
                        newFile = new File(directory, filename + "[" + i + "].jpeg");
                        i++;
                    }
                    mypath=newFile;
                }
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(mypath);
                    bitmapImage.compress(Bitmap.CompressFormat.JPEG, 85, fos);
                    return true;
                } catch (Exception e) {
                    Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
                    return false;//e.printStackTrace();
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
                        return false;//e.printStackTrace();
                    }
                }
            }
            else
                return false;
        }
        else
            return false;
}

No comments:

Post a Comment