Need help with Intent !?

362 views
Skip to first unread message

RuslanX

unread,
May 13, 2016, 5:52:27 PM5/13/16
to DroidScript
Hi, I need to use default camera to take photo, so I use intent to capture image ... but how to receive the file path as result ... anybody ?

used code from samples :
//Called when application is started.
function OnStart()
{
   
//Create a layout with objects vertically centered.
    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );    
   
   
//Create a button 1/3 of screen width and 1/10 screen height.
    btn
= app.CreateButton( "Send Intent", 0.3, 0.1 );
    btn
.SetOnTouch( btn_OnTouch );
    lay
.AddChild( btn );
   
       
//Create a text label and text boxt.
    txt
= app.CreateText( "Intent Data:" );
    lay
.AddChild( txt );
    txt1
= app.CreateText( "", 0.95, 0.6 , "left,multiline" );
    txt1
.SetBackColor( "#ff222222" );
    txt1
.SetTextSize( 18 );
    lay
.AddChild( txt1 );

   
//Add layout to app.    
    app
.AddLayout( lay );
}

//Called when user touches our button.
function btn_OnTouch()
{
   
// var packageName = "com.android.camera";
   
// var className = "";
   
// var action = "MediaStore.ACTION_IMAGE_CAPTURE";
   
// var category = null;
   
// var uri = "myfr...@gmail.com";
   
// var type = "message/rfc822";
   
   
// var extras = [
   
//     {name:"android.intent.extra.EMAIL", type:"list", value:"fr...@gmail.com"},
   
//     {name:"android.intent.extra.SUBJECT", type:"string", value:"My subject"},
   
//     {name:"android.intent.extra.TEXT", type:"string", value:"Hello!"}
   
// ];
   
// extras = JSON.stringify( extras );


    app
.SendIntent(null,null,'android.media.action.IMAGE_CAPTURE',null,null,null,null,null, myCallback);
}

function myCallback(e)
{
    app
.ShowPopup(e);
}

function OnData( isStartUp )
{
       
//Display intent data.
   
var intent = app.GetIntent();
   
if( intent )
   
{
       
//Extract main data.
       
var s = "action: " + intent.action + "\n";
        s
+= "type: " + intent.type + "\n";
        s
+= "data: " + intent.data + "\n\n";
       
       
//Extract extras.
        s
+= "extras:\n";
       
for( var key in intent.extras )
            s
+= key+": "+intent.extras[key] + "\n";
       
        txt1
.SetText( s );
   
}
}

how can I get file path as result ?

here my old post with same
question : https://groups.google.com/forum/#!searchin/androidscript/intent/androidscript/yC_z4l1dz7o/c5P-ctsRAgAJ
Message has been deleted

RuslanX

unread,
May 13, 2016, 6:15:56 PM5/13/16
to DroidScript
in java:

// Sent Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (i.resolveActivity(getPackageManager()!=null)){
 
try{
 cacheFile
= createTempFile("img",".jpg",getCacheDir());
 cacheFile
.setWritavle(true,false);
 
}catch(IOException e){}
 
if(cacheFile != null){
 i
.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(cacheFile));
 startActivityForResult
(i,REQUEST_IMAGE_CAPTURE);
 
}
}


// Receive Intent
protected void onActivityResult(int requestCode,int resultCode,Intent data){
 
if(requestCode != RESULT_OK){
 
return;
 
}
 
if(requestCode == REQUEST_IMAGE_CAPTURE){
 
try{
 
File output = getImageFile();
 
if(output != null && cacheFile != null){
 copyFile
(cacheFile,output);

 
//Process image file stored at output

 cacheFile
.delete();
 cacheFile
=null;
 
}
 
}catch(IOException e){}
 
}
}


sankarshan dudhate

unread,
May 14, 2016, 2:33:21 AM5/14/16
to DroidScript
At the moment, I don't think it's possible. I suggest you to try the latest alpha which might return the filepath.

Regards,
Sankarshan

RuslanX

unread,
May 14, 2016, 5:14:07 AM5/14/16
to DroidScript
Where I can download latest alpha version ?

RuslanX

unread,
May 14, 2016, 5:22:29 AM5/14/16
to DroidScript
I can try to capture image with camera using intent and then to find recent image in gallery and copy to my working folder then delete from gallery ... is this method reliable ? ... and is a simple way to get recent image in gallery ?

Dave Smart

unread,
May 14, 2016, 5:42:56 AM5/14/16
to DroidScript
You need to join the beta group to get information about the alpha versions.  Let me know if you want to join and I will add your email address.

Francesco I.

unread,
May 16, 2016, 2:40:37 AM5/16/16
to DroidScript
Hi!

if you can wait a couple of days , and if you desire it , I am going to release a GalleryPick plugin , that provides some methods to work with the intent of the gallery ... and I might add the intent to take the picture for you as needed ...

RuslanX

unread,
May 17, 2016, 3:46:28 AM5/17/16
to DroidScript
Wow, thank you .. will wait ... can't finish my app ..

Francesco I.

unread,
May 18, 2016, 3:59:07 PM5/18/16
to DroidScript
Hi!

I finished the plugin ( including TakePicture function you need) ! Tomorrow morning ( now here are the 22 o'clock ) I finish the documentation and release the plugin and the demo application ...

Stay tuned! :)

RuslanX

unread,
May 18, 2016, 5:51:17 PM5/18/16
to DroidScript

Francesco I.

unread,
May 19, 2016, 8:06:00 AM5/19/16
to DroidScript
Hi!

I have released GalleryPick plugin (in new forum thread)...

you can download and test/use it...

RuslanX

unread,
May 19, 2016, 8:57:39 AM5/19/16
to DroidScript
Hi, cool plugin !

... I put GalleryPick.zip.dat in Plugins (created) folder ... on Droidscript start the Plugin is installed .. when I try to run example from docs  .. got "Script Error: app.CreateGaleryPick is not a function" ... what is wrong ?

Francesco I.

unread,
May 19, 2016, 9:10:59 AM5/19/16
to DroidScript
1. have you renamed GalleryPick.zip.dat in GalleryPick.zip before copy into /DroidScript/Plugins folder?
2. have you write app.CreateGalleryPick or app.CreateGaleryPick (as reported into your post)
3. can you try with app.CreateObject ("GalleryPick")
4. can you try one of documentation examples? (click on docs > GalleryPick > Function ... > Example ...)

RuslanX

unread,
May 19, 2016, 9:12:16 AM5/19/16
to DroidScript
Sorry, I dont renamed the file (extension)  :) ...

RuslanX

unread,
May 19, 2016, 9:17:02 AM5/19/16
to DroidScript
now is OK ... but after I take picture (from doc example) I got Error:uri

Francesco I.

unread,
May 19, 2016, 9:32:54 AM5/19/16
to DroidScript
the example works like this:
1. at the click of button, opens the camera
2. at the camera shutter, shows Ok - Cancel
3. pressing Ok, it calls the View function that displays the image.

at what point it give you the error?

before or after the camera is opened?
it gives you error immediately after the Ok button press?
the picture just taken is present in the gallery if you search outside?

can you post your test device (manufacturer / model)?

RuslanX

unread,
May 19, 2016, 9:39:48 AM5/19/16
to DroidScript
it gives immediately after the Ok button press ... is not present in the gallery.

Francesco I.

unread,
May 19, 2016, 9:52:41 AM5/19/16
to DroidScript
I dont know... I try to understand... what is your android version?

RuslanX

unread,
May 19, 2016, 9:53:43 AM5/19/16
to DroidScript
Nexus 5, Android 6.01

RuslanX

unread,
May 19, 2016, 10:00:03 AM5/19/16
to DroidScript
 /**
  * Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/>
  * Will return the correct URI for camera and gallery image.
  *
  * @param data the returned data of the activity result
  */

 
public Uri getPickImageResultUri(Intent data) {
     
boolean isCamera = true;
     
if (android.os.Build.VERSION.SDK_INT >= 23) {
         isCamera
= (data != null && data.getClipData() != null) ? false : true;
     
} else {
         
if (data != null) {
             
String action = data.getAction();
             isCamera
= action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
         
}
     
}
     
return isCamera ? getCaptureImageOutputUri() : data.getData();
 
}

Based on the Sample from @ArthurHub, this fix works on Nexus 5 | Android 6.0.1

Add permisson to the AndroidManifest.xml and replace the getPickImageResultUri method.

<uses-permission android:name="android.permission.CAMERA"/>


finded here : https://github.com/ArthurHub/Android-Image-Cropper/issues/26

RuslanX

unread,
May 19, 2016, 10:05:04 AM5/19/16
to DroidScript
The problem that I am facing, is that I do not know why the APP can not load the specified URI in both versions (5.0.2 and 6.0.1). On 5.0.2 it uses the default location File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURE‌​S), "MyCameraApp"); but on 6.0.1 it crashes. So I changed this to mediaFile = File.createTempFile(timeStamp, extension, mWebViewActivity.getExternalCacheDir());. The problem is temporary solved as it does not work on 6.0.1, it can not find the Uri, on 5.0.2 it uses the default Uri and stores the data there. – Thanos Feb 17 at 11:23
       
    
try : File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).g‌​etAbsolutePath() + "/" + "your_filename_with_extension");



source: http://stackoverflow.com/questions/35454750/android-6-0-1-can-not-make-directory-environment-getexternalstoragepublicdirect

Francesco I.

unread,
May 19, 2016, 10:06:53 AM5/19/16
to DroidScript
Ok! I'll try to integrate this code... Please wait...

RuslanX

unread,
May 19, 2016, 10:29:37 AM5/19/16
to DroidScript

Francesco I.

unread,
May 19, 2016, 10:58:08 AM5/19/16
to DroidScript
Ok... I have integrate your posted code... If you want, you can try plugin attached here (I cannot becouse I have all android 4.x)

If you want try, put this file (renamed as you have done before) into DroidScript/Plugins folder, then restart DroidScript (plugon will be replaced)

I waiting for news...

GalleryPick.zip.dat

RuslanX

unread,
May 19, 2016, 11:16:01 AM5/19/16
to DroidScript


RuslanX

unread,
May 19, 2016, 11:20:26 AM5/19/16
to DroidScript
I think, needs permissions for Android 6.01, look link above

RuslanX

unread,
May 19, 2016, 11:24:37 AM5/19/16
to DroidScript

Francesco I.

unread,
May 19, 2016, 3:00:58 PM5/19/16
to DroidScript
If problem are permission, I can't help you... Plugins inherits permissions from DroidScript... You can ask into forum for this...

Steve Garman

unread,
May 19, 2016, 3:48:55 PM5/19/16
to DroidScript
If you want to add permissions to the app or to the plugin, this post is helpful. https://groups.google.com/d/msg/androidscript/ygHEA_tnXTc/KgSH77KnAwAJ

_AddPermissions() can be placed at the start of the plugin .inc file or in the app's own code.

Francesco I.

unread,
May 20, 2016, 1:41:01 AM5/20/16
to DroidScript
Thanks Steve!

RuslanX, I have added permissions (Camera and Storage) as described by Steve... If you can try, I attach modified version here... Same instrucrions for installation

I waiting...

GalleryPick.zip.dat

RuslanX

unread,
May 20, 2016, 5:40:02 AM5/20/16
to DroidScript
Hi, same error as above ...

Here how must be to work camera on Android 6.0+ (Marshmallow):


For Checking permission create a separate class as below:

public class MarshMallowPermission {

   
public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
   
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
   
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
   
Activity activity;

   
public MarshMallowPermission(Activity activity) {
       
this.activity = activity;
   
}

   
public boolean checkPermissionForRecord(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
       
} else {
           
return false;
       
}
   
}

   
public boolean checkPermissionForExternalStorage(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
       
} else {
           
return false;
       
}
   
}

   
public boolean checkPermissionForCamera(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
       
} else {
           
return false;
       
}
   
}

   
public void requestPermissionForRecord(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
           
Toast.makeText(activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
       
} else {
           
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
       
}
   
}

   
public void requestPermissionForExternalStorage(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
           
Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
       
} else {
           
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
       
}
   
}

   
public void requestPermissionForCamera(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
           
Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
       
} else {
           
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
       
}
   
}
}








Then, for getting Camera Picture :

...
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
...

public void getPhotoFromCamera() {

   
if (!marshMallowPermission.checkPermissionForCamera()) {
        marshMallowPermission
.requestPermissionForCamera();
   
} else {
       
if (!marshMallowPermission.checkPermissionForExternalStorage()) {
            marshMallowPermission
.requestPermissionForExternalStorage();
       
} else {
           
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           
File mediaStorageDir = new File(
                   
Environment.getExternalStorageDirectory()
                           
+ File.separator
                           
+ getString(R.string.directory_name_corp_chat)
                           
+ File.separator
                           
+ getString(R.string.directory_name_images)
           
);

           
if (!mediaStorageDir.exists()) {
                mediaStorageDir
.mkdirs();
           
}

           
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                   
Locale.getDefault()).format(new Date());
           
try {
                mediaFile
= File.createTempFile(
                       
"IMG_" + timeStamp,  /* prefix */
                       
".jpg",         /* suffix */
                        mediaStorageDir      
/* directory */
               
);
                takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
                startActivityForResult
(takePictureIntent, PICK_FROM_CAMERA);
           
} catch (IOException e) {
                e
.printStackTrace();
           
}
       
}
   
}
}



Can you try adjust code to fit in your Plugin ?




RuslanX

unread,
May 20, 2016, 5:41:35 AM5/20/16
to DroidScript
Hmm, code was cutted ..

RuslanX

unread,
May 20, 2016, 5:43:23 AM5/20/16
to DroidScript

RuslanX

unread,
May 20, 2016, 5:51:39 AM5/20/16
to DroidScript
Reply all
Reply to author
Forward
0 new messages