Problem with Android Path and Image
-
So i want make a program that can show image from internal Android files (so i dont need to update the apk to show/ + new image, user can just give and pick himself) with you as user will just type your name and show it, to make it happen i need to know with path it is, for that i make a "show image with you pick it" APK.
it goes like this in qt widget#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QPixmap> #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // Dialog to choose an image QString filePath = QFileDialog::getOpenFileName(this, "Open Image", "/storage/emulated/0", "Images (*.png *.xpm *.jpg)"); if (!filePath.isEmpty()) { // Set the file path to the label for displaying path ui->labelFilePath->setWordWrap(true); ui->labelFilePath->setText(filePath); QPixmap pixmap(filePath); if (!pixmap.isNull()) { ui->label->setPixmap(pixmap.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); } } } MainWindow::~MainWindow() { delete ui; }
it is do well but the output is something like content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fdio.jpg, instead something like /sdcard/Download/dio.jpg fyi this was the real path that i can use too, that i found it in android studio
after that i make the show one who like this
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QPixmap> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QString filename = ui->filename->text(); // Tambahkan pengecekan panjang teks jika diperlukan if (filename.length() < 1) { QMessageBox::warning(this, "Input Error", "Please enter at least 1 characters for the filename."); return; } //QString imagePath = "content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F"+filename+".jpg"; QString imagePath = "content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fjoko.jpg"; //QString imagePath = "content://com.android.providers.media.documents/document/image%3A" + filename + ""; //19,20,21 //QString imagePath = "document/raw:/storage/emulated/0/Download/"+filename+".jpg"; QImage image(imagePath); if (!image.isNull()) { QImage scaledImage = image.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); // Ubah QImage ke QPixmap QPixmap pixmap = QPixmap::fromImage(scaledImage); // Pastikan pixmap tidak null if (!pixmap.isNull()) { ui->label->setFixedSize(pixmap.size()); ui->label->setPixmap(pixmap); ui->label->update(); } else { QMessageBox::warning(this, "Load Error", "Failed to convert image."); } } else { QMessageBox::warning(this, "Load Error", "The image could not be loaded. Please check the filename and file path."); } } //QString imagePath = "/sdcard/Download/"+filename+".jpg"; //content://com.android.providers.media.documents/document/image%3A21
the problem was
- it never show your typing if not 12 char++
- when you press button, it just show only half, you need click the line edit to make it full
the setup was, android 9, SDK 28, JDK 19.0.2.7, with kit Android 6.7 x86
FYI i make first program with qtQuick Cmake, and its complete mess (force close), and i make the second program with QtQuick Qmake and it do fine is path is ok like (sdcard/Download/"+filename+".jpg) but not work with (Content://...)
- it never show your typing if not 12 char++
-