Android onActivityResult从未发布

Android onActivityResult从未发布,android,cordova,android-intent,phonegap-plugins,Android,Cordova,Android Intent,Phonegap Plugins,很抱歉再次提出同样的问题,但在寻找答案后,我没有找到一个正常工作的代码 ñ这是我调用活动的主要功能: package com.phonegap.CameraPluginTest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.phon

很抱歉再次提出同样的问题,但在寻找答案后,我没有找到一个正常工作的代码

ñ这是我调用活动的主要功能:

package com.phonegap.CameraPluginTest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.phonegap.DroidGap;

public class CameraPluginTest2_3Activity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.loadUrl("file:///android_asset/www/index.html");
        try{
            CustomCamera camera = new CustomCamera(this);
            appView.addJavascriptInterface(camera, "CustomCamera");
            //camera.Cameratest();
            //camera.startCamera(50);
        }catch(Exception ex){
            ex.printStackTrace();
            Log.e("RapportsError",  "error" + ex.toString());
        }
    }

    public class CustomCamera{
        private Context context;

        public CustomCamera (Context context){
            this.context = context;

        }

        public void Cameratest() {
            Log.e("RapportsError",  "Cameratest");

        }
        public void startCamera(int quality)
        {
            Log.e("RapportsError",  "intent");
            try{

                Intent i = new Intent(context, CameraPreview.class);

                i.setAction("android.intent.action.PICK");
                i.putExtra("quality", quality);
                startActivityForResult(i, 1);
                Log.e("RapportsError",  "despues de intent" );
            }catch(Exception ex){
                ex.printStackTrace();
                Log.e("RapportsError",  "error" + ex.toString());
            }
        }

        protected void onActivityResult(int reqCode, int resCode, Intent intent)
        {
            String data;            
            Log.e("RapportsError",  "ON ACTIVITY RESULT " + reqCode);
            //super.onActivityResult(requestCode, resultCode, intent);
            if (resCode == RESULT_OK)
            {
                data = intent.getStringExtra("picture"); 
                Log.e("RapportsError",  "error" + data);
                // Send the graphic back to the class that needs it
                //launcher.processPicture(data);
            }
            else
            {
                //launcher.failPicture("Did not complete!");
            }
        }

    }

}
这就是所谓的类

package com.phonegap.CameraPluginTest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.util.List;

import org.apache.commons.codec.binary.Base64;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import android.graphics.YuvImage;
import android.os.Environment;

public class CameraPreview extends Activity implements SurfaceHolder.Callback{

    private static final String TAG = "PhoneGapCamera";
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

    private RelativeLayout root;

    Camera mCamera;
    boolean mPreviewRunning = false;

    int quality;
    Intent mIntent;

    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        Log.e(TAG, "onCreate");

        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                ViewGroup.LayoutParams.FILL_PARENT);
        LinearLayout.LayoutParams surfaceParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                ViewGroup.LayoutParams.FILL_PARENT, 0.0F);
        RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT);

        root = new RelativeLayout(this);
        root.setLayoutParams(containerParams);

        mSurfaceView = new SurfaceView(this);
        mSurfaceView.setLayoutParams(surfaceParams);
        root.addView(mSurfaceView);

        Button stopButton = new Button(this);
        stopButton.setText("click");
        buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        buttonParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        buttonParams.rightMargin = 5;
        buttonParams.topMargin = 5;

        stopButton.setLayoutParams(buttonParams);
        root.addView(stopButton);

        setContentView(root);

        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);                    
        mIntent = this.getIntent();

        quality = mIntent.getIntExtra("quality", 100);


        stopButton.setOnClickListener(mSnapListener);
    }

    private OnClickListener mSnapListener = new OnClickListener() {
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPictureCallback);
        }
    };

    //public boolean onCreateOptionsMenu(android.view.Menu menu) {
    //    MenuItem item = menu.add(0, 0, 0, "goto gallery");
    //    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
    //        public boolean onMenuItemClick(MenuItem item) {
    //            Uri target = Uri.parse("content://media/external/images/media");
    //            Intent intent = new Intent(Intent.ACTION_VIEW, target);
    //            startActivity(intent);
    //            return true;
    //        }
    //    });
    //    return true;
    //}

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);        
    }

    /*
     * We got the data, send it back to PhoneGap to be handled and processed.
     * 
     */

   // Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
   //     public void onPictureTaken(byte[] data, Camera c) {
   //         Log.e(TAG, "PICTURE CALLBACK: data.length = " + data.length);
   //         storeAndExit(data);

   //     }
    //};

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {        
        public void onPictureTaken(byte[] data, Camera _camera) {                        
              storeAndExit(data,_camera);
              Log.d(TAG, "onPictureTaken - jpeg");                            
        }
      };

    /*
     * We can't just store and exit, because Android freezes up when we try to cram a picture across a process in a Bundle.
     * We HAVE to compress this data and send back the compressed data  
     */
    public void storeAndExit(byte[] data,Camera _camera)
    {       
          FileOutputStream outStream = null;          
          String fotoName = new String();
          try {
          fotoName="wy"+System.currentTimeMillis();
          outStream = new FileOutputStream(String.format(
                  "/sdcard/%s.png", fotoName));
          outStream.write(data);
          outStream.close();
          Log.e(TAG, "PICTURE CALLBACK: fotoName" + fotoName + "data.length = " + data.length);
          String js_out = new String( "/sdcard/" + fotoName + ".png");

          mIntent.putExtra("picture", js_out);
              //setResult(RESULT_OK, mIntent);
          this.setResult(RESULT_OK, mIntent);
         // Uri imageUri = Uri.fromFile(outStream);
         // storeAndExit(data);
              finish();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
          }           
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) {         
            return super.onKeyDown(keyCode, event);
        }

        if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_SEARCH) {
            mCamera.takePicture(null, null, mPictureCallback);
            return true;
        }

        return false;
    }

    protected void onResume()
    {
        Log.e(TAG, "onResume");
        super.onResume();
    }

    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
    }

    protected void onStop()
    {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();
        //mCamera.startPreview();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
    {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();

        Camera.Size previewSize = previewSizes.get(0);


        p.setPreviewSize(previewSize.width, previewSize.height);
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    public void onPreviewFrame(byte[] data, Camera camera) {
        try {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();
            YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
                    size.width, size.height, null);
            File file = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/out.jpg");
            FileOutputStream filecon = new FileOutputStream(file);
            image.compressToJpeg(
                    new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
                    filecon);
        } catch (FileNotFoundException e) {
            Toast toast = Toast
                    .makeText(getBaseContext(), e.getMessage(), 1000);
            toast.show();
        }
    }


}
package com.phonegap.CameraPluginTest;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
导入org.apache.commons.codec.binary.Base64;
导入android.app.Activity;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.PixelFormat;
导入android.graphics.Bitmap.CompressFormat;
导入android.graphics.Rect;
导入android.hardware.Camera;
导入android.hardware.Camera.Size;
导入android.net.Uri;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.MenuItem;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.LinearLayout;
导入android.widget.RelativeLayout;
导入android.widget.Toast;
导入android.graphics.YuvImage;
导入android.os.Environment;
公共类CameraPreview扩展活动实现SurfaceHolder.Callback{
私有静态最终字符串TAG=“PhoneGapCamera”;
私人SurfaceView mSurfaceView;
私人地表持有人;
私有相对根;
麦克默拉照相机;
布尔值mPreviewRunning=false;
内在品质;
意向性薄荷糖;
创建公共空间(捆绑冰柱)
{
超级冰柱;
Log.e(标记为“onCreate”);
getWindow().setFormat(像素格式.半透明);
RelativeLayout.LayoutParams containerParams=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL\u父级,
ViewGroup.LayoutParams.FILL\u父级);
LinearLayout.LayoutParams surfaceParams=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL\u父级,
ViewGroup.LayoutParams.FILL\u父对象,0.0F);
RelativeLayout.LayoutParams ButtonParms=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_内容);
root=新的RelativeLayout(本);
root.setLayoutParams(containerParams);
mSurfaceView=新的SurfaceView(此);
mSurfaceView.setLayoutParams(surfaceParams);
root.addView(mSurfaceView);
按钮停止按钮=新按钮(此按钮);
stopButton.setText(“单击”);
ButtonParms.addRule(相对长度对齐\u父项\u右键);
ButtonParms.addRule(相对长度对齐\u父项\u顶部);
ButtonParms.rightMargin=5;
ButtonParms.topMargin=5;
stopButton.setLayoutParams(按钮图);
root.addView(停止按钮);
setContentView(根目录);
mSurfaceHolder=mSurfaceView.getHolder();
mSurfaceHolder.addCallback(此);
mSurfaceHolder.setType(SurfaceHolder.SURFACE类型推送缓冲区);
minent=this.getIntent();
质量=minent.getIntExtra(“质量”,100);
setOnClickListener(mSnapListener);
}
私有OnClickListener mSnapListener=新OnClickListener(){
公共void onClick(视图v){
takePicture(null,null,mPictureCallback);
}
};
//公共布尔onCreateOptions菜单(android.view.Menu菜单){
//MenuItem item=menu.add(0,0,0,“转到画廊”);
//item.setOnMenuItemClickListener(新的MenuItem.OnMenuItemClickListener(){
//公共布尔onMenuItemClick(菜单项){
//Uri目标=Uri.parse(“content://media/external/images/media");
//意向意向=新意向(意向.行动\视图,目标);
//星触觉(意向);
//返回true;
//        }
//    });
//返回true;
//}
@凌驾
RestoreInstanceState上的受保护无效(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
/*
*我们得到了数据,将其发送回PhoneGap进行处理。
* 
*/
//Camera.PictureCallback mPictureCallback=新建Camera.PictureCallback(){
//公共void onPictureTaken(字节[]数据,摄像头c){
//Log.e(标签,“图片回调:data.length=“+data.length”);
//storeAndExit(数据);
//     }
//};
Camera.PictureCallback mPictureCallback=新建Camera.PictureCallback(){
公共void onPictureTaken(字节[]数据,摄像头_摄像头){
存储和退出(数据,_摄像头);
Log.d(标签“onPictureTaken-jpeg”);
}
};
/*
*我们不能只是存储和退出,因为当我们试图将一张图片塞进一个捆绑包的过程中时,Android会死机。
*我们必须压缩这些数据并将压缩后的数据发回
*/
public void storeAndExit(字节[]数据,摄像头_摄像头)
{       
FileOutputStream扩展流=null;
字符串fotoName=新字符串();
试一试{
fotoName=“wy”+System.currentTimeMillis();
outStream=新文件OutputStream(String.format(
“/sdcard/%s.png”,fotoName));
外扩。写入(数据);
exptream.close();
Log.e(标记,“图片回调:fotoName”+fotoName+“data.length=“+data.length”);
String js_out=新字符串(“/sdcard/”+fotoName+”.png”);
Minent.putExtra(“图片”,js_out);
//设置结果(结果正常,分钟);
此.setResult(结果\u OK,MINENT);
//Uri imageUri=Uri.fromFile(超出流);
//storeAndExit(数据);
完成();
}捕获(文件)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phonegap.CameraPluginTest"
    android:versionCode="1"
    android:versionName="1.0" >

<supports-screens 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:resizeable="true" 
    android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true" >
        <activity
            android:name=".CameraPluginTest2_3Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CameraPreview" 
            android:label="CameraPreviewLabel" 
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape">
        </activity>                
    </application>

</manifest>
this.cordova.setActivityResultCallback(MyActivity.this);
android:launchMode="singleInstance"
public class MainActivity extends CordovaActivity {
                    //or 'extends DroidGap' based on Cordova version

    //...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == MyCustomActivity) {
                    doMyCustomActivity(requestCode);
            }
            else { //call onActivityResult() of the plugins
                super.onActivityResult(requestCode, resultCode, intent);
            }
    }

    //...
}