Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Problem with Android Path and Image
Forum Update on Monday, May 27th 2025

Problem with Android Path and Image

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 253 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patar
    wrote on 13 May 2024, 06:51 last edited by
    #1

    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

    1. it never show your typing if not 12 char++
      940b5b3d-3cbc-48be-b746-bcc25937e7a7-image.png f666bf03-c9be-41e2-bd2d-f3c1d892032c-image.png
    2. when you press button, it just show only half, you need click the line edit to make it full
      a8a303ad-f125-45d5-805e-50b661328696-image.png 01076946-206a-44c2-af6a-f03d96ec9af8-image.png

    the setup was, android 9, SDK 28, JDK 19.0.2.7, with kit Android 6.7 x86
    e1192281-1b33-46e1-a143-a4c2dae478ab-image.png

    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://...)

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Patar
      wrote on 21 May 2024, 00:52 last edited by
      #2

      Done it, i turn around and do with qt quick

      1 Reply Last reply
      0
      • P Patar has marked this topic as solved on 21 May 2024, 00:53

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved