Open image camera or gallery selector from main QtActivity

This commit is contained in:
Angela Mazzurco 2017-07-11 17:35:38 +02:00
parent 3b092f7fb6
commit 5838b13043
2 changed files with 73 additions and 4 deletions

View File

@ -19,17 +19,24 @@
package org.retroshare.android.qml_app;
import android.app.ActivityManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.net.Uri;
import org.qtproject.qt5.android.bindings.QtActivity;
import org.retroshare.android.qml_app.jni.NativeCalls;
public class RetroShareQmlActivity extends QtActivity
{
static final int PICK_PHOTO = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
@ -73,4 +80,67 @@ public class RetroShareQmlActivity extends QtActivity
return true;
return false;
}
private Uri capturedImageURI;
public void openImagePicker()
{
Log.i("RetroShareQmlActivity", "openImagePicker()");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Retroshare Avatar");
capturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageURI);
Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {takePicture});
startActivityForResult( chooserIntent, PICK_PHOTO);
};
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("RetroShareQmlActivity", "onActivityResult()" + String.valueOf(requestCode));
if (resultCode == RESULT_OK)
{
if (requestCode == PICK_PHOTO)
{
final boolean isCamera;
if (data == null)
{
isCamera = true;
}
else
{
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = capturedImageURI;
} else {
selectedImageUri = data == null ? null : data.getData();
}
Log.i("RetroShareQmlActivity", "Image uri found!" + selectedImageUri.toString());
}
}
}
}

View File

@ -19,10 +19,9 @@ public slots:
qDebug() << "Starting image picker intent";
#ifdef __ANDROID__
QAndroidJniObject::callStaticMethod<void>(
"org/retroshare/android/qml_app/RetroshareImagePicker",
"imagePickerIntent",
"()Landroid/content/Intent;" );
QtAndroid::androidActivity().callMethod<void>(
"openImagePicker",
"()V" );
#endif // __ANDROID__
}
};