0

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

相机.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;
 } 

重新加载照片活动后,在启动我的应用程序时,它显示“等待调试......”。我犯了什么错误?谁能帮帮我吗...

4

1 回答 1

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);
    }

}

清单文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-05-10T05:37:38.423 回答