I have done a code to capture image by using camera in my qt app. I am using QtAndroid to write a equivalent java code in Qt.
I have already opened the camera, captured the image, but uri returns null and suddenly app crash on above android pie version phones, and it properly work on android lolipop phones.
How can I get uri of captured image using QAndroid to write a Java equivalent code in Qt?
Here is the code I have done:
Code for opening camera :-
QAndroidJniObject ACTION_IMAGE_CAPTURE = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "ACTION_IMAGE_CAPTURE", "Ljava/lang/String;");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_IMAGE_CAPTURE.object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), 101, this);
Code for getting image uri :-
void UiMan::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject
&data)
{
qDebug()<<"In handelActivityResult";
jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
if (receiverRequestCode == 101 && resultCode == RESULT_OK)
{
qDebug()<<"In If";
QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
QAndroidJniEnvironment env;
jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str());
env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid);
QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, NULL, NULL, NULL);
jint columnIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
cursor.callMethod<jboolean>("moveToFirst", "()Z");
QAndroidJniObject resultado = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
QFile imageFile(resultado.toString());
QDir dir;
if(dir.exists("images")){
qDebug()<<"Folder already exists";
}else {
dir.mkdir("images");
}
QStringList splitedImagePath = resultado.toString().split("/");
QString imageName = splitedImagePath.value(splitedImagePath.length() - 1);
QString dataimgFile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/images/" + imageName;
// /data/data/org.qtproject.feather/files/images/offLight_Button.png
if (imageFile.exists())
{
QFile cdbfile(dataimgFile);
if (!cdbfile.exists())
{
if(!imageFile.copy(dataimgFile))
qDebug() << "Error :copy failed" ;
QFile::setPermissions(dataimgFile, QFile::WriteOwner | QFile::ReadOwner);
}
else
{
qDebug() << "image exits allready no need to copy " ;
QFile::setPermissions(dataimgFile, QFile::WriteOwner | QFile::ReadOwner);
}
}
else
{
qDebug() << "img Not exit in Assert " << dataimgFile;
}
QVariant imagePath = "file://" + dataimgFile;
emit signalToQml(1148, imagePath, "", "","","","","","");
}
else
{
qDebug() << "Error";
}
}
So. please help me for solving this problem. and help me to write equivalent qt code is given below for getting bitmap of captured image,
Bitmap bitmap = (Bitmap) data.getExtras().get("data");