Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "HEIC to JPEG Converter";
- private static final int REQUEST_PICK_HEIC_FILE = 1;
- private static final int REQUEST_PICK_JPEG_FILE = 2;
- private Button mPickHeicButton;
- private Button mPickJpegButton;
- private File mHeicFile;
- private File mJpegFile;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mPickHeicButton = findViewById(R.id.pick_heic_button);
- mPickJpegButton = findViewById(R.id.pick_jpeg_button);
- mPickHeicButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // Open the file picker dialog to select the input HEIC file
- Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
- intent.addCategory(Intent.CATEGORY_OPENABLE);
- intent.setType("image/heic");
- startActivityForResult(intent, REQUEST_PICK_HEIC_FILE);
- }
- });
- mPickJpegButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // Open the file picker dialog to select the output JPEG file
- Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
- intent.addCategory(Intent.CATEGORY_OPENABLE);
- intent.setType("image/jpeg");
- intent.putExtra(Intent.EXTRA_TITLE, "output.jpg");
- startActivityForResult(intent, REQUEST_PICK_JPEG_FILE);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- if (requestCode == REQUEST_PICK_HEIC_FILE) {
- // Get the selected HEIC file
- if (data != null && data.getData() != null) {
- mHeicFile = new File(getFilePathFromUri(data.getData()));
- Log.d(TAG, "Selected HEIC file: " + mHeicFile.getAbsolutePath());
- }
- } else if (requestCode == REQUEST_PICK_JPEG_FILE) {
- // Get the selected JPEG file
- if (data != null && data.getData() != null) {
- mJpegFile = new File(getFilePathFromUri(data.getData()));
- Log.d(TAG, "Selected JPEG file: " + mJpegFile.getAbsolutePath());
- }
- }
- // Check if both files are selected and convert HEIC to JPEG
- if (mHeicFile != null && mJpegFile != null) {
- HEICToJPEGConverter converter = new HEICToJPEGConverter();
- converter.convert(mHeicFile.getAbsolutePath(), mJpegFile.getAbsolutePath());
- Log.d(TAG, "Conversion complete.");
- }
- }
- }
- private String getFilePathFromUri(Uri uri) {
- String filePath = null;
- String[] projection = {MediaStore.Images.Media.DATA};
- Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
- if (cursor != null && cursor.moveToFirst()) {
- int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- filePath = cursor.getString(columnIndex);
- cursor.close();
- }
- return filePath;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment