Android:在img标签中显示捕获的图像

时间:2012-05-09 05:56:21

标签: javascript android html

我想在html页面的img标签中显示捕获的图像。我可以通过OnActivityResult事件中的 data.getData()获取URI值。但我不知道我可以发送图像并以img标签显示的格式。请任何人帮助m

Camera.java:

  public class Camera extends Activity 
    {
         private static final int CAMERA_REQUEST = 1888;
         private String selectedImagePath;
         WebView webview;
         String fileName = "capturedImage.jpg";
         private static Uri mCapturedImageURI; 

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
                webview=(WebView)findViewById(R.id.webView1);
        }

        public void TakePhoto()
        {   
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Images.Media.TITLE, fileName);  
                mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }       
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode == RESULT_OK)
                {
                  if (requestCode == CAMERA_REQUEST) 
                  { 
                    selectedImagePath = getPath(mCapturedImageURI);
                webview.loadUrl("javascript:ReceivePhoto(\""+selectedImagePath+"\")");
                  }
                }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

    }

HTML:

 function CallBack(resultimage) 
 {
       myimg.src = resultimage;
 } 

拍摄照片后,重新加载&在启动我的应用程序时,它显示“正在等待调试......”。我做错了什么?任何人都可以帮助我...

1 个答案:

答案 0 :(得分:0)

有一个解决方案。 : - )

public class Camera extends Activity 
{
     private static final int CAMERA_REQUEST = 1888;
     private String selectedImagePath;
     WebView webview;
     String fileName = "capturedImage.jpg";
     FileOutputStream fo;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
            webview=(WebView)findViewById(R.id.webView1);
    }

    public void TakePhoto()
    {   

            Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }       
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK)
            {
              if (requestCode == CAMERA_REQUEST) 
              { 
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
            Random randomGenerator = new Random();randomGenerator.nextInt(100);
            String newimagename=randomGenerator.toString()+".jpg";
            File f = new File(Environment.getExternalStorageDirectory()
                                    + File.separator + newimagename);
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //write the bytes in file

            try {
                fo = new FileOutputStream(f.getAbsoluteFile());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            webview.loadUrl("javascript:ReceivePhoto(\""+f.getAbsolutePath()+"\")");
              }
            }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

}

manifest资源配置文件:

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