sadiqul_amin

Android app

Feb 27th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private static final String TAG = "HEIC to JPEG Converter";
  4. private static final int REQUEST_PICK_HEIC_FILE = 1;
  5. private static final int REQUEST_PICK_JPEG_FILE = 2;
  6.  
  7. private Button mPickHeicButton;
  8. private Button mPickJpegButton;
  9.  
  10. private File mHeicFile;
  11. private File mJpegFile;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. mPickHeicButton = findViewById(R.id.pick_heic_button);
  19. mPickJpegButton = findViewById(R.id.pick_jpeg_button);
  20.  
  21. mPickHeicButton.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. // Open the file picker dialog to select the input HEIC file
  25. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  26. intent.addCategory(Intent.CATEGORY_OPENABLE);
  27. intent.setType("image/heic");
  28. startActivityForResult(intent, REQUEST_PICK_HEIC_FILE);
  29. }
  30. });
  31.  
  32. mPickJpegButton.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View view) {
  35. // Open the file picker dialog to select the output JPEG file
  36. Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
  37. intent.addCategory(Intent.CATEGORY_OPENABLE);
  38. intent.setType("image/jpeg");
  39. intent.putExtra(Intent.EXTRA_TITLE, "output.jpg");
  40. startActivityForResult(intent, REQUEST_PICK_JPEG_FILE);
  41. }
  42. });
  43. }
  44.  
  45. @Override
  46. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  47. super.onActivityResult(requestCode, resultCode, data);
  48. if (resultCode == RESULT_OK) {
  49. if (requestCode == REQUEST_PICK_HEIC_FILE) {
  50. // Get the selected HEIC file
  51. if (data != null && data.getData() != null) {
  52. mHeicFile = new File(getFilePathFromUri(data.getData()));
  53. Log.d(TAG, "Selected HEIC file: " + mHeicFile.getAbsolutePath());
  54. }
  55. } else if (requestCode == REQUEST_PICK_JPEG_FILE) {
  56. // Get the selected JPEG file
  57. if (data != null && data.getData() != null) {
  58. mJpegFile = new File(getFilePathFromUri(data.getData()));
  59. Log.d(TAG, "Selected JPEG file: " + mJpegFile.getAbsolutePath());
  60. }
  61. }
  62.  
  63. // Check if both files are selected and convert HEIC to JPEG
  64. if (mHeicFile != null && mJpegFile != null) {
  65. HEICToJPEGConverter converter = new HEICToJPEGConverter();
  66. converter.convert(mHeicFile.getAbsolutePath(), mJpegFile.getAbsolutePath());
  67. Log.d(TAG, "Conversion complete.");
  68. }
  69. }
  70. }
  71.  
  72. private String getFilePathFromUri(Uri uri) {
  73. String filePath = null;
  74. String[] projection = {MediaStore.Images.Media.DATA};
  75. Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
  76. if (cursor != null && cursor.moveToFirst()) {
  77. int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  78. filePath = cursor.getString(columnIndex);
  79. cursor.close();
  80. }
  81. return filePath;
  82. }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment